前言
一直沒機會做spring生態(tài)圈的框架模蜡,公司選擇的是一些小眾的微服務(wù),鑒于此考慮,豐富自己的技術(shù)棧亡容,花了兩天時間從網(wǎng)上各網(wǎng)站上學(xué)習(xí)了springboot一些基礎(chǔ)知識。
本章只介紹springboot微服務(wù)集成swagger2冤今,用于后端工程師開發(fā)過程中調(diào)試接口闺兢。
環(huán)境準備
- IntelliJ IDEA
- 前一章中搭建的微服務(wù)框架
開始集成
-
pom.xml中增加依賴包
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
-
引入swaggerUI靜態(tài)資源文件,將資源拷貝到resources/static目錄下戏罢,否則會報404屋谭,找不到資源,資源下載路徑見://download.csdn.net/download/xuefei2290819/12043546
-
與DemoApplication.java同級目錄新建SwaggerConfig類
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
/**
* 類功能描述:<br>
* <ul>
* <li>類功能描述1<br>
* <li>類功能描述2<br>
* <li>類功能描述3<br>
* </ul>
* 修改記錄:<br>
* <ul>
* <li>修改記錄描述1<br>
* <li>修改記錄描述2<br>
* <li>修改記錄描述3<br>
* </ul>
*
* @author xuefl
* @version 5.0 since 2019-12-19
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
// 解決 SWAGGER 404報錯
registry.addResourceHandler("/static/index.html").addResourceLocations("classpath:/META-INF/resources/static/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())//生成文檔的api對象定義
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))//掃描生成文檔的包路徑
//.paths(PathSelectors.ant("/*Api/*"))//生成文檔的類訪問路徑龟糕,就是controller類里@RequestMapping("orderApi")
.paths(PathSelectors.any())
.build();
//.host(Host);//配置swagger前綴
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot-demo構(gòu)建restful接口api")//文檔標題
.description("此API提供接口調(diào)用")//文檔說明
.version("2.0").build();//版本號
}
}
-
DemoApplication中增加@EnableSwagger2注解
package com.example.demo;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@MapperScan("com.example.demo.mapper")
@EnableScheduling
@EnableTransactionManagement
@Slf4j
public class DemoApplication {
public static void main(String[] args) {
log.info("DemoApplication run enter...");
SpringApplication.run(DemoApplication.class, args);
}
}
- 運行DemoApplication桐磁,啟動服務(wù),訪問 http://localhost:8081/api/