一、pom中引入jar
2.8.0
io.springfox
springfox-swagger2
${swagger2.version}
io.springfox
springfox-swagger-ui
${swagger2.version}
二、配置swagger信息
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build()
.globalResponseMessage(RequestMethod.GET,
newArrayList(new ResponseMessageBuilder()
.code(500)
.message("系统繁忙!")
.build(),
new ResponseMessageBuilder()
.code(200)
.message("请求成功!")
.build()))
.globalResponseMessage(RequestMethod.POST,
newArrayList(new ResponseMessageBuilder()
.code(500)
.message("系统繁忙!")
.build(),
new ResponseMessageBuilder()
.code(200)
.message("请求成功!")
.build()))
.globalResponseMessage(RequestMethod.DELETE,
newArrayList(new ResponseMessageBuilder()
.code(500)
.message("系统繁忙!")
.build(),
new ResponseMessageBuilder()
.code(200)
.message("请求成功!")
.build()));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API接口文档")
.description("")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}
三、统一返回格式
@ApiModel(value = "返回说明")
public class ResultData{
@ApiModelProperty(value = "成功标识;true:成功;false:失败")
private boolean success;
@ApiModelProperty(value = "返回状态码;200:成功")
private String code;
@ApiModelProperty(value = "描述信息")
private String message;
private T data;
}
四、controller中使用注解
@Api(value = "登录接口")
public class UserController{
@ApiOperation(value = "登录", notes = "登录")
@PostMapping(value = "/doLogin")
public ResultData doLogin(LoginAppVo loginAppVo){
.....
}
}
五、vo中标注参数信息
@ApiModel
public class LoginAppVo{
@ApiModelProperty(value="用户名", required = true, dataType = "String")
private String userName;
@ApiModelProperty(value="用户密码", required = true, dataType = "String")
private String password;
}
结果: