在Springboot項目中使用Swagger文檔
使用步驟
1.添加依賴
<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>
2.添加配置類
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.lee.demo.controller"))
.paths(PathSelectors.any())
.build()
// .enable(false)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("測試swagger2")
.version("v1.0")
.description("API 測試文檔")
.build();
}
}
3.註解接口類
- tags屬性——描述接口信息
@Api(tags = "xxx接口")
4.註解接口方法
@ApiOperation("xxx")
@ApiImplicitParam(paramType = "xxx", name = "xxx", value = "xxx", required = true)
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNum", value = "頁碼", defaultValue = "1"),
@ApiImplicitParam(name = "pageSize", value = "頁尺寸", defaultValue = "20"),
@ApiImplicitParam(name = "personName", value = "人員姓名")
})
@ApiIgnore
@ApiParam
5.註解實體類
@ApiModel(value = "xxx", description = "xxx")
@ApiModelProperty(value = "員工id")
6.訪問接口文檔
http://localhost:8080/swagger-ui.html
接口過濾
- 使用
@ApiIgnore
- 在Docket上增加篩選
apis()
:可以通過指定包名的方式,讓 Swagger 只去某些包下面掃描。paths()
:可以通過篩選 API 的 url 來進行過濾。
生產環境中禁用swagger
- 取消配置類中下面的註釋,重啓後再訪問接口文檔如圖所示
// .enable(false) 項目上線時關閉接口文檔
- 或者直接指定環境
@Profile("dev")
問題1
當多個接口的請求路徑相同,靠參數來區分時,swagger ui只能顯示任意其中一個接口
問題2
- 沒有在代碼中配置swagger時,會出現以下異常,配置後刪除target重新rebuild即可解決
- 參考博客
問題3
- AbstractSerializableParameter : Illegal DefaultValue null for parameter type integer
2018-10-24 23:03:36.537 WARN 19699 --- [nio-1111-exec-4] i.s.m.p.AbstractSerializableParameter : Illegal DefaultValue null for parameter type integer
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_171]
at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_171]
at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_171]
at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412) ~[swagger-models-1.5.20.jar:1.5.20]
at sun.reflect.GeneratedMethodAccessor109.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:687) [jackson-databind-2.9.6.jar:2.9.6]
-
原因:這是由於實體類使用@ApiModelProperty時,example屬性沒有賦值導致的,在AbstractSerializableParameter的getExample方法中會將數值屬性的example的轉換數值類返回,example的默認值是"",因此當example沒有賦值時,會出現上面的異常。getExample方法如下
@JsonProperty("x-example") public Object getExample() { if (example == null) { return null; } try { if (BaseIntegerProperty.TYPE.equals(type)) { return Long.valueOf(example); } else if (DecimalProperty.TYPE.equals(type)) { return Double.valueOf(example); } else if (BooleanProperty.TYPE.equals(type)) { if ("true".equalsIgnoreCase(example) || "false".equalsIgnoreCase(defaultValue)) { return Boolean.valueOf(example); } } } catch (NumberFormatException e) { LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", defaultValue, type), e); } return example; }
-
解決方法:將每一個數值類型上
@ApiModelProperty
的example都賦值數字字符串即可 -
參考文章:https://blog.csdn.net/weixin_38229356/article/details/83353347
問題4
-
訪問接口文檔遇到404
-
可能的原因:跨域
-
解決方法:https://blog.csdn.net/xtj332/article/details/80595768
-
試試用127.0.0.1代替localhost
參考博客
-
https://www.ibm.com/developerworks/cn/java/j-using-swagger-in-a-spring-boot-project/index.html
-
https://blog.csdn.net/itguangit/article/details/78978296
-
https://dzone.com/articles/spring-boot-2-restful-api-documentation-with-swagg
-
https://dzone.com/articles/spring-boot-restful-api-documentation-with-swagger
-
https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api