2022
我们一起努力

如何实现knife4j导出离线接口文档 - 编程语言

本篇内容主要讲解“如何实现knife4j导出离线接口文档”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何实现knife4j导出离线接口文档”吧!

** 导出离线接口文档,妈妈再也不用让我手动写接口文档了 **

  1. 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>`
  1. 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导出离线接口文档 - 编程语言

到此,相信大家对“如何实现knife4j导出离线接口文档”有了更深的了解,不妨来实际操作一番吧!这里是云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

赞(0)
文章名称:《如何实现knife4j导出离线接口文档 - 编程语言》
文章链接:https://www.fzvps.com/87238.html
本站文章来源于互联网,如有侵权,请联系管理删除,本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
图片版权归属各自创作者所有,图片水印出于防止被无耻之徒盗取劳动成果的目的。

评论 抢沙发

评论前必须登录!