knife4j只用此插件的最简洁开发方式
一、POM添加
在pom文件里添加包
1 <!--引入knife4j以来--> 2 <dependency> 3 <groupId>com.github.xiaoymin</groupId> 4 <artifactId>knife4j-spring-boot-starter</artifactId> <version>1.9.6</version> 5 </dependency>
二、配置添加,相当于<bean>添加
1 package com.mrliu.undertow.conf; 2 3 import com.github.xiaoymin.knife4j.spring.annotations.EnableSwaggerBootstrapUi; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import springfox.documentation.builders.ApiInfoBuilder; 7 import springfox.documentation.builders.PathSelectors; 8 import springfox.documentation.builders.RequestHandlerSelectors; 9 import springfox.documentation.service.ApiInfo; 10 import springfox.documentation.service.Contact; 11 import springfox.documentation.spi.DocumentationType; 12 import springfox.documentation.spring.web.plugins.Docket; 13 import springfox.documentation.swagger2.annotations.EnableSwagger2; 14 15 /** 16 * @author liuyang 17 */ 18 @Configuration 19 @EnableSwagger2 20 @EnableSwaggerBootstrapUi 21 public class Swagger2Config { 22 23 /** 24 * 创建连接的包信息 25 * <p> 26 * 配置统一返回的controller路径RequestHandlerSelectors.basePackage 27 * 28 * @return 返回创建状况 29 */ 30 @Bean 31 public Docket createRestApi() { 32 return new Docket(DocumentationType.SWAGGER_2) 33 .useDefaultResponseMessages(false) 34 .apiInfo(apiInfo()) 35 .select() 36 .apis(RequestHandlerSelectors.basePackage("com.mrliu.undertow.controller")) 37 .paths(PathSelectors.any()) 38 .build(); 39 40 } 41 42 43 /** 44 * 设置文档信息主页的内容说明 45 * 46 * @return 文档信息 47 */ 48 private ApiInfo apiInfo() { 49 return new ApiInfoBuilder() 50 .title("Project textBook API ") 51 .description("服务接口") 52 .termsOfServiceUrl("http://localhost:8001/") 53 .contact(new Contact("Mr Liu", "http://localhost:8999/", "liuyang@synway.cn")) 54 .license("what") 55 .version("1.0") 56 .build(); 57 } 58 59 }
apiInfo()设置后会改变如图
三、bean实体类添加
1 /** 2 * @author Administrator 3 */ 4 @ApiModel("用户对象") 5 @Data 6 public class UserVO { 7 8 @ApiModelProperty(required = true, notes = "用户名", example = "blues") 9 private String name; 10 11 @ApiModelProperty(required = true, notes = "用户返回消息", example = "hello world") 12 private String words; 13 14 15 public UserVO(String name, String words) { 16 this.name = name; 17 this.words = words; 18 } 19 }
主要是添加注解
@ApiModel("用户对象") -----实体类注解添加 添加后结果如图所示:
@ApiModelProperty(required = true, notes = "用户名", example = "blues") 字段注解添加 添加后结果如图所示:
四、Controller添加
- 1、GET方式访问
1 package com.mrliu.undertow.controller; 2 3 import com.mrliu.undertow.base.Results; 4 import com.mrliu.undertow.pojo.UserVO; 5 import io.swagger.annotations.*; 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.PathVariable; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 import java.io.IOException; 14 15 /** 16 * @author Administrator 17 */ 18 @Api(tags = "HELLO CONTROLLER 测试功能接口") 19 @RestController 20 public class HelloController { 21 22 23 @ApiImplicitParams({ 24 @ApiImplicitParam(name = "name",value = "用户名称",required = true,dataType = "String",paramType = "path",example = "blues") 25 }) 26 @ApiResponses(value = { 27 @ApiResponse(code = 200, message = "接口返回成功状态"), 28 @ApiResponse(code = 500, message = "接口返回未知错误,请联系开发人员调试") 29 }) 30 @ApiOperation(value = "Hello 测试接口", notes = "访问此接口,返回hello语句,测试接口") 31 @GetMapping("hello/{name}") 32 public Results<UserVO> hello(@PathVariable String name){ 33 UserVO userVO = new UserVO(name,"hello " + name); 34 Results<UserVO> results = new Results<>(200,"SUCCESS", userVO); 35 return results; 36 } 37 }
- 2、POST方式访问
1 @Api(tags = "HELLO CONTROLLER 测试功能接口") 2 @RestController 3 public class HelloController { 4 5 6 @ApiImplicitParams({ 7 @ApiImplicitParam(name = "name",value = "用户名称",required = true,dataType = "String",paramType = "path",example = "blues") 8 }) 9 @ApiResponses(value = { 10 @ApiResponse(code = 200, message = "接口返回成功状态"), 11 @ApiResponse(code = 500, message = "接口返回未知错误,请联系开发人员调试") 12 }) 13 @ApiOperation(value = "Hello 测试接口", notes = "访问此接口,返回hello语句,测试接口") 14 @PostMapping("hello/{name}") 15 public Results<UserVO> hello(@RequestBody UserVO userVO){ 16 Results<UserVO> results = new Results<>(200,"SUCCESS", userVO); 17 return results; 18 } 19 }
五、访问URL
http://localhost:7788/doc.html
六、兼容swagger-ui访问
http://localhost:7788/swagger-ui.html
七、测试
填入参数
对象请求访问
八、API文档复制
复制后可生成markdow文档,使用showdoc,即可翻译成文档,下载html、PDF、word等格式
源码地址:https://github.com/liushaoye/knife4j.git 欢迎点赞,分享,推荐
十年磨一剑,一剑破万法