How to implement knife4j Export offline Interface document
This article focuses on "how to achieve knife4j export offline interface documents", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to implement knife4j export offline interface documents"!
* * to export offline interface documents, my mother no longer has to ask me to write interface documents manually * *
1 introduce dependency
Io.springfox springfox-boot-starter 3.0.0 com.github.xiaoymin knife4j-spring-boot-starter 3.0.2 `
2 add swagger configuration class
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 configuration * * @ author xmtx * / @ Configuration@EnableOpenApi@EnableKnife4jpublic class Swagger3Config {@ Bean public Docket systemApi () {return new Docket (DocumentationType.SWAGGER_2) .grou pName ("Xiaoming Children's shoes demo") .genericModelSubstitutes (DeferredResult.class) .useDefaultResponseMessages (false) .forCodeGeneration (true) .select () .apis ( RequestHandlerSelectors.withClassAnnotation (Api.class)) / / the path where controller is written here. Apis (RequestHandlerSelectors.basePackage ("com.example.demo.controller")) .path (PathSelectors.any ()) .build () .pathMappin g ("/") / / No authorization authentication / / .securitySchemes (Collections.singletonList (securitySchema () / / .securityConcepts (Collections.singletonList (securityContext () .apiInfo (systemApiInfo ()) } private ApiInfo systemApiInfo () {return new ApiInfoBuilder () .title ("Xiaoming Children's shoes demo") .description ("Test swagger integrates knife4j to generate offline interface documentation") .termsOfServiceUrl ("https://my.oschina.net/xiaomingnevermind") .contact (new Contact (" xmtx ",") "xmtx.2015@gmail.com") .version ("1.0") .build () } / * * generate global common parameter * * @ return * / private List getGlobalRequestParameters () {List parameters = new ArrayList () Parameters.add (new RequestParameterBuilder () .name ("x-access-token") .description ("token") .required (false) .in (ParameterType.HEADER) .build () Parameters.add (new RequestParameterBuilder () .name ("Equipment-Type") .description ("product type") .required (false) .in (ParameterType.HEADER) .build (); return parameters } / * generate general response information * * @ return * / private List getGlobalResponseMessage () {List responseList = new ArrayList (); responseList.add (new ResponseBuilder (). Code ("404"). Description ("Resource not found"). Build (); return responseList;} / private OAuth securitySchema () {/ List authorizationScopeList = new ArrayList () / / List 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 authorizationScopes apiUnix *")) / / .build (); / /} private List defaultAuth () {final AuthorizationScope [] authorizationScopes = new AuthorizationScope [0] Return Collections.singletonList (new SecurityReference ("oauth3schema", authorizationScopes);}}
1. 3. Write a controller for testing
Private static final Logger log = LoggerFactory.getLogger (AspectController.class); @ Autowiredprivate AspectService aspectService;@ApiOperation (value = "test getaspect") @ GetMapping (value = "/ getaspect") public String getAspect (@ ApiParam ("name") 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. View the effect picture
At this point, I believe that everyone on "how to achieve knife4j export offline interface documents" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!