1.引入依赖配置
<!--集成SWAGGER-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
2.添加配置config
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
// 分组
.groupName("main")
.select()
// api的包路径
.apis(RequestHandlerSelectors.basePackage("com.xiaoyoung"))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 文档标题
.title("API Doc")
// 文档描述
.description("接口描述")
// 版本号
.version("1.0")
.build();
}
}
3.配置适配路径,否则会报错
#配置swagger
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
4.添加swagger注解
4.1、类注解
注解 | 说明 |
---|---|
@Api | 对请求类的说明 |
4.2、方法注解
注解 | 说明 |
---|---|
@ApiOperation | 方法的说明 |
@ApiImplicitParams | 方法参数的说明; |
@ApiImplicitParam | 用于指定单个参数的说明。 |
4.3、方法的响应状态的描述
注解 | 说明 |
---|---|
@ApiResponses | 方法返回值的说明 ; |
@ApiResponse | 用于指定单个参数的说明。 |
4.4、对象的描述
注解 | 说明 |
---|---|
@ApiModel | 用在JavaBean类上,说明JavaBean的 整体用途 |
@ApiModelProperty | 用在JavaBean类的属性上面,说明此属性的的含议 |
效果图: