引入依赖
<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>
添加扫描
@EnableSwagger2
访问SwaggerUI
localhost:8080/SwaggerUi.html
Swageer 配置
@Configuration
public class SwaggerConfiguration{
@Bean
public Docket docket(){
Docker docker = new Docket(DocumentationType.SWAGGER_2);
ApiInfo apiInfo =
new ApiInfoBuilder()
.contact(
new Contact(
"文档名字",
"网址",
"email"
)
)
.title("名称")
.descripiton("描述")
.version("1.1")
.build();
docker.apiInfo(apiInfo);
docker.select()
.apis(Predicates.not(RequestHandleSelectors.withMethodAnnotation(MyAnnotation4Swagger)))
.apis(RequestHandlerSelecrors.basePackage(""));
.paths(PathSelectors.regex("/swagger/.*"))
return docker;
}
}
自定义注解不生成文档
@Target(ElementType.METHOD,ElementType.TYPE)
@Retention("RententionPolicy.RUNTIME")
public @interface MyAnnotation4Swagger{
String value() default "",
}
常用自带注解
@Api(tags={"MyColler","Swagger学习控制器"},description="描述")
@ApiOperation(value="描述" notes="标记")
@ApiParam(name="参数名称",value="描述")
@ApiIgnor()
@ApiImplicitParam(name = "m",value = "m参数描述")
@ApiImplicitParams( value = {
@ApiImplicitParam(name = "m",value = "m参数描述")
@ApiImplicitParam(name = "n",value = "n参数描述")
})
@ApiModel(value="自定义实体-MyEntity",description="MyEntity")
@ApiModeProperty(value = "参数名",name = "描述",required="是否必要",example="实例",hidden = "是否隐藏")