您好,登錄后才能下訂單哦!
本篇內容主要講解“如何實現knife4j導出離線接口文檔”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何實現knife4j導出離線接口文檔”吧!
** 導出離線接口文檔,媽媽再也不用讓我手動寫接口文檔了 **
1 引入依賴
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-boot-starter --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency>`
2 增加swagger配置類
package com.example.demo.conf; import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import io.swagger.annotations.Api; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.request.async.DeferredResult; import springfox.documentation.builders.*; import springfox.documentation.oas.annotations.EnableOpenApi; import springfox.documentation.service.*; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spring.web.plugins.Docket; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * swagger 配置 * * @author xmtx */ @Configuration @EnableOpenApi @EnableKnife4j public class Swagger3Config { @Bean public Docket systemApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName("小明童鞋demo") .genericModelSubstitutes(DeferredResult.class).useDefaultResponseMessages(false).forCodeGeneration(true) .select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) // 這里寫controller 所在的路徑 .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()).build() .pathMapping("/") // 暫時不加權限認證 // .securitySchemes(Collections.singletonList(securitySchema())) // .securityContexts(Collections.singletonList(securityContext())) .apiInfo(systemApiInfo()); } private ApiInfo systemApiInfo() { return new ApiInfoBuilder() .title("小明童鞋demo") .description("測試swagger整合knife4j生成離線接口文檔") .termsOfServiceUrl("https://my.oschina.net/xiaomingnevermind") .contact(new Contact("xmtx", "", "xmtx.2015@gmail.com")) .version("1.0") .build(); } /** * 生成全局通用參數 * * @return */ private List<RequestParameter> getGlobalRequestParameters() { List<RequestParameter> parameters = new ArrayList<>(); parameters.add(new RequestParameterBuilder() .name("x-access-token") .description("令牌") .required(false) .in(ParameterType.HEADER) .build()); parameters.add(new RequestParameterBuilder() .name("Equipment-Type") .description("產品類型") .required(false) .in(ParameterType.HEADER) .build()); return parameters; } /** * 生成通用響應信息 * * @return */ private List<Response> getGlobalResponseMessage() { List<Response> responseList = new ArrayList<>(); responseList.add(new ResponseBuilder().code("404").description("找不到資源").build()); return responseList; } // // private OAuth securitySchema() { // // List<AuthorizationScope> authorizationScopeList = new ArrayList(); // List<GrantType> grantTypes = new ArrayList(); // GrantType creGrant = new ResourceOwnerPasswordCredentialsGrant("/oauth/token"); // // grantTypes.add(creGrant); // // return new OAuth("oauth3schema", authorizationScopeList, grantTypes); // // } // // private SecurityContext securityContext() { // return SecurityContext.builder() // .securityReferences(defaultAuth()) // .forPaths(PathSelectors.ant("/v1/api/**")) // .build(); // } private List<SecurityReference> defaultAuth() { final AuthorizationScope[] authorizationScopes = new AuthorizationScope[0]; return Collections.singletonList(new SecurityReference("oauth3schema", authorizationScopes)); } }
1. 3. 編寫一個controller進行測試
private static final Logger log = LoggerFactory.getLogger(AspectController.class); @Autowired private AspectService aspectService; @ApiOperation(value = "測試getaspect") @GetMapping(value = "/getaspect") public String getAspect(@ApiParam("名稱") String name, @ApiParam Integer age) throws InterruptedException { AspectBean aspectBean = new AspectBean(); aspectBean.setAge(age); aspectBean.setBirthday(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date().getTime())); aspectBean.setSex(1); aspectBean.setName(name); return JSON.toJSONString(aspectService.testAspect(aspectBean)); } @PostMapping(value = "/postaspect") public String postAspect(@RequestBody AspectBean aspectBean) throws InterruptedException { return JSON.toJSONString(aspectService.testAspect(aspectBean)); } @GetMapping(value = "/init") public boolean init() { return aspectService.init(); }
1. 4. 查看效果圖
到此,相信大家對“如何實現knife4j導出離線接口文檔”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。