在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