1、配置Pom.xml
引入Swagger的依赖
<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>
2.x版本会报错NumberFormatException,更改版本即可解决
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.22</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.22</version>
</dependency>
2、配置Swagger
1、配置Swagger的配置文件,标注@Configuration注解
2、在主配置类上开启自动配置,标注@EnableSwagger2
3、启动项目访问/swagger-ui.html
3、定制Swagger
1、默认Swagger有一个实例Docket
2、创建一个Bean覆盖默认的
@Bean
public Docket docket(){
Docket docket = new Docket(DocumentationType.SWAGGER_2);
docket.apiInfo(apiInfo())
.groupName("w x x")
.select()
.apis(RequestHandlerSelectors.basePackage("com.wx.swagger.controller"))
.build();
return docket;
}
public ApiInfo apiInfo(){
ApiInfo build = new ApiInfoBuilder().title("MyTitle")
.description("MyDescription")
.termsOfServiceUrl("www.baidu.com")
.contact(new Contact("My","www.baidu.com","123123@qq.com"))
.license("MyLicense")
.licenseUrl("MyLicenseUrl")
.version("V12.0")
.build();
return build;
}
4、Swagger常用注解
@Api 用于类名上 示例:@Api(value = "xxxController",descripition="xxx相关操作")
description已被弃用
@ApiOperation 用于方法上 示例:@ApiOperation("xxx操作")
@ApiParam 用于方法的参数上 示例:@ApiParam(name="xxx属性",value="xxx格式",required=true)
@ApiModel 用于实体类上
@ApiModelProperty 用于实体类属性上