1.pom文件加入knife4j依赖
#包含spring mvc依赖
< dependency>
< groupId>org.springframework.boot
< artifactId>spring-boot-starter-web
< /dependency>
#knife4j依赖
< dependency>
< groupId>com.github.xiaoymin
< artifactId>knife4j-spring-boot-starter
< version>3.0.2
< /dependency>
2.配置knife4j
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) //文档版本
.apiInfo(apiInfo()) //api配置对象,下方有实现。
.select()
.apis(RequestHandlerSelectors.basePackage(“com.shuaih.haha”)) //api所在路径,即controller层所在包
.paths(PathSelectors.any()) //请求路径此处配置的所有路径等同于/*。
.build();
}
// api对象
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger-bootstrap-ui RESTful APIs") //文档头部
.description("swagger-bootstrap-ui") //文档描述
.termsOfServiceUrl("http://localhost:8080/") //服务地址
.version("1.0") //版本
.build();
}
}
3.启动项目,浏览器访问http://localhost:8080/doc.html#
4.swagger注解
@Api(value = “登录服务”,description = “用户登录相关接口”)
Api 用在Controller中,标记一个Controller作为swagger的文档资源
@ApiOperation(value = “获取验证码图片”,notes = “每调用一次,就会随机生成一张验证码图片”,response = String.class)
ApiOperation 该注解用在Controller的方法中,用于注解接口
@ApiParam(name = “param”,value = “实体类AclParam”,required = true)
ApiParam 该注解用在方法的参数中。
@ApiResponses({
@ApiResponse(code = 200,message = “成功!”),
@ApiResponse(code = 401,message = “未授权!”),
@ApiResponse(code = 404,message = “页面未找到!”),
@ApiResponse(code = 403,message = “出错了!”)
})
ApiResponses/ApiResponse 该注解用在Controller的方法中,用于注解方法的返回状态。
@ApiModel(value = “用户信息类”,description = “用户信息”)
ApiModel 该注解用在实体类中。
@ApiModelProperty(name = “count”,value = “查询数量”,notes = “返回信息的条数”)
ApiModelProperty 该注解用在实体类的字段中。