使用前配置:
1.引入依赖:
<!-- swagger在线接口文档-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.xiaoymin</groupId>-->
<!-- <artifactId>swagger-bootstrap-ui</artifactId>-->
<!-- <version>1.9.6</version>-->
<!-- </dependency>-->
2.防止启动报错,配置:
#Swagger3
spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
配置类:
@Configuration
public class Swagger3Config {
@Bean
public Docket getDocket(){
return new Docket(DocumentationType.OAS_30).apiInfo(getApiInfo());
/*return new Docket(DocumentationType.OAS_30)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();*/
}
@Bean
public ApiInfo getApiInfo(){
return new ApiInfoBuilder()
.title("XXX系统-接口文档")
.description("这是sdy!!!!")
.contact(new Contact("sdy","http://www.baidu.com","1656088120@qq.com"))
.version("v1.0")
.build();
}
}
3.使用:
实例:
在接口中:
@RestController
//此类接口说明
@Api(tags = "账户管理接口")
public class Swagger3Controller {
@RequestMapping("/swagger/test1")
// 该接口说明
@ApiOperation("登录接口")
// 参数说明
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "username",value = "用户名",required = true,dataType = "String"),
@ApiImplicitParam(name = "password",value = "密码",required = true,dataType = "String"),
@ApiImplicitParam(name = "token",value = "token",required = true,dataType = "String",paramType = "header"),
})
// 返回值说明
@ApiResponses(value = {
@ApiResponse(code = 400,message = "xxx错误"),
@ApiResponse(code = 300,message = "xxx错误")
})
public Client test1(String username, String password, @RequestHeader String token){
return new Client();
}
@RequestMapping("/swagger/test2")
@ApiOperation("查询年龄接口")
@ApiImplicitParam(name = "token",value = "token",required = true,dataType = "int")
public String test2(int age){
return "OK";
}
}
实体类:
@Data
@ApiModel("客户类")
public class Client {
@ApiModelProperty(name = "cname",value = "客户名")
private String cname;
@ApiModelProperty(name = "money",value = "账户余额")
private double money;
}