04 Swagger
Swagger 提供了一套通过代码和注解自动生成文档的方法。
添加依赖
<!-- 自动生成描述 API 的 json 文件 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 将 json 文件解析出来,用一种更友好的方式呈现 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>配置参数
swagger:
title: API标题
description: API描述
version: 1.0.0
terms-of-service-url: https://chanshiyu.com/
base-package: com.chanshiyu
contact:
name: 蝉時雨
url: https://chanshiyu.com/
email: me@chanshiyu.com配置类
@Profile({"dev", "test"}) 只在开发和测试环境启用 swagger。
使用
@Api
描述一个 API 类
@ApiImplicitParam
描述一个请求参数
@ApiImplicitParams
描述一组请求参数
@ApiModel
描述一个返回的对象
@ApiModelProperty
描述一个返回的对象参数
@ApiOperation
描述一个 API 方法
@ApiParam
描述一个方法的参数
@ApiResponse
描述一个请求响应
@ApiResponses
描述一组请求响应
@ApiIgnore
表示忽略
ApiImplicitParam 与 ApiParam 的区别:
对 Servlets 或者非 JAX-RS 的环境,只能使用
ApiImplicitParam。在使用上,
ApiImplicitParam比ApiParam具有更少的代码侵入性,只要写在方法上就可以了,但是需要提供具体的属性才能配合 swagger ui 解析使用。ApiParam只需要较少的属性,与 swagger ui 配合更好。
启动项目访问路径查看文档:http://192.168.51.242:8080/swagger-ui.html#/。
Cannot resolve configuration property
解决步骤:
添加依赖
spring-boot-configuration-processor依赖添加
src/main/resources/META-INF/spring-configuration-metadata.json文件
首先添加 spring-boot-configuration-processor 依赖:
之后添加 src/main/resources/META-INF/spring-configuration-metadata.json 文件:
最后更新于
这有帮助吗?