SpringBoot整合Swagger2,3分鐘輕鬆入手!
阿新 • • 發佈:2020-08-23
### 一、引入maven
```xml
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
```
2.9.2版本,該版本有個小坑後面會提到
### 二、建立一個Swagger配置類
```java
@Configuration
@EnableSwagger2//開啟Swagger2的自動配置
@Profile({"dev", "pred"})
public class SwaggerConfig {
@Bean
public Docket peopleDeptApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo("人才庫", "人才庫-人員,部門介面文件", "1.0"))
.select()
.apis(RequestHandlerSelectors.basePackage("com.allqj.platform_base_organization.base.controller"))
.paths(PathSelectors.any())
.build()
.groupName("人員,部門API");
}
@Bean
public Docket brokerApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo("人才庫", "人才庫-經紀人介面文件", "1.0"))
.select()
.apis(RequestHandlerSelectors.basePackage("com.allqj.platform_base_organization.broker.controller"))
.paths(PathSelectors.any())
.build()
.groupName("經紀人API");
}
@Bean
public Docket dictionaryApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo("人才庫", "人才庫-字典介面文件", "1.0"))
.select()
.apis(RequestHandlerSelectors.basePackage("com.allqj.platform_base_organization.dictionary.controller"))
.paths(PathSelectors.any())
.build()
.groupName("字典API");
}
private ApiInfo apiInfo(String title, String description, String version) {
return new ApiInfoBuilder().title(title).description(description).version(version).build();
}
}
```
### 三、防止中文分組亂碼
在application.yml檔案裡配置
```yml
spring:
http:
encoding:
charset: UTF-8
force: true
enabled: true
server:
tomcat:
uri-encoding: UTF-8
```
如果還是讀取不到,清除瀏覽器快取
### 四、分組配置就好了,訪問專案路徑/swagger-ui.html就ok了
### 五、修復 swaggerfox 升級 2.9.2版本問題
開始的時候我有提到:整合Swagger新版本中,有一個小坑。這裡具體說明下,當我們使用版本2.9.2時候,如果專案實體中有Integer型別的屬性,當我們開啟Api文件的時候會出現一個警告資訊:
```java
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_221]
at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_221]
at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_221]
at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412) ~[swagger-models-1.5.20.jar:1.5.20]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:687) [jackson-databind-2.9.7.jar:2.9.7]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) [jackson-databind-2.9.7.jar:2.9.7]
```
##### 解決方案
排除springfox-swagger2 引入的 swagger-models 1.5.20版本,手動引入1.5.21版本的jar。具體原因可以檢視如下連結,這篇文章說的很詳細!
> https://blog.csdn.net/qq122516902/article/details/89673363
pom.xml配置如下
```xml
io.springfox
springfox-swagger2
2.9.2
swagger-models
io.swagger
swagger-models
io.swagger
1.5.21
```