knife4j使用文档
引入依赖:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-micro-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
配置:
/**
* Swagger文档,只有在测试环境才会使用
*/
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfiguration {
@Bean
public Docket baseRestApi() {
return new Docket(D ocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.ldk.controller")).paths(PathSelectors.any())
.build();
}
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder().title("接口测试文档").description("接口文档Swagger版").termsOfServiceUrl("")
.contact(new Contact("", "", "")).version("1.0").build();
}
}
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
@Api(tags = "系统用户模块")
public class SysUserController {
@GetMapping("/test/{name}")
@ApiImplicitParam(name = "name",value = "姓名",required = true)
@ApiOperation(value = "向客人问好")
public String test(@PathVariable String name) {
}
访问路径:http://localhost:17790/doc.html
项目中SpringBoot集成Swagger,Spring已经将Swagger纳入自身的标准,建立了Spring-swagger项目,现在叫
Springfox。通过在项目中引入Springfox ,即可非常简单快捷的使用Swagger。
在Controller接口方法上使用注解描述接口内容,Swagger常用注解如下:
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数的描述信息
@ApiModel:用对象来接收参数
@ApiModelProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数的描述信息