添加pom:
io.springfox
springfox-swagger2
2.7.0
io.springfox
springfox-swagger-ui
2.7.0
创建工具类SwaggerConfig
@Configuration
@EnableSwagger2
public class swaggerConfig {
@Bean
public Docket webApiConfig(){
return new Docket(documentationType.SWAGGER_2)
.groupName("webApi").apiInfo(webApiInfo()).select()
.paths(Predicates.not(PathSelectors.regex("/admin/.*")))
.paths(Predicates.not(PathSelectors.regex("error.*"))).build();
}
private ApiInfo webApiInfo() {
return new ApiInfoBuilder().title("springboott整合swagger2测试")
.description("springboott整合swagger2测试").version("1.0")
.contact(new Contact("test","https://localhost","aaa@qq.com"))
.build();
}
}
类中添加相应的注解:
定义在类上:@Api
定义在方法上:@ApiOperation
定义在参数上:@ApiParam
@Api: 修饰整个类,用于controller类上
@ApiOperation: 描述一个接口,用户controller方法上
@ApiParam: 单个参数描述
@ApiModel: 用来对象接收参数,即返回对象
@ApiModelProperty: 对象接收参数时,描述对象的字段
@ApiResponse: Http响应其中的描述,在ApiResonse中
@ApiResponses: Http响应所有的描述,用在
@ApiIgnore: 忽略这个API
@ApiError: 发生错误的返回信息
@ApiImplicitParam: 一个请求参数
@ApiImplicitParam: 多个请求参数
在启动启动类中添加
@ComponentScan(basePackages = {"com.joe"})
最后启动访问
http://localhost:8080/swagger-ui.html
完事



