maven坐标:
<!--swagger-->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
配置类:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public static final String VERSION = "1.0.0";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))//api接口包扫描路径
.paths(PathSelectors.any())//可以根据url路径设置哪些请求加入文档,忽略哪些请求
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SmartItApp后台接口文档")//设置文档的标题
.description("详细接口内容请找开发人员确定")//设置文档的描述->1.Overview
.version(VERSION)//设置文档的版本信息-> 1.1 Version information
.build();
}
}
使用的注解:
@Api("告警模块") //修饰类
@ApiOperation(value = "方法描述",produces = "application/json") //修饰方法
@ApiImplicitParams({ //入参描述
@ApiImplicitParam(name="username",value = "用户名 必传",dataType = "String"),
@ApiImplicitParam(name="password",value = "密码 必传",dataType = "String")
})
@ApiModel(value = "User",description = "用户名") //入参实体类描述,放在类上
@ApiModelProperty(value = "请求的页大小 必传",dataType = "Integer",example = "5") //入参字段描述,放在字段上
访问地址: