溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何實(shí)現(xiàn)knife4j導(dǎo)出離線接口文檔

發(fā)布時間:2021-10-12 15:26:11 來源:億速云 閱讀:1808 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“如何實(shí)現(xiàn)knife4j導(dǎo)出離線接口文檔”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何實(shí)現(xiàn)knife4j導(dǎo)出離線接口文檔”吧!

** 導(dǎo)出離線接口文檔,媽媽再也不用讓我手動寫接口文檔了 **

  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("/")
                // 暫時不加權(quán)限認(rèn)證
//                .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();
    }

    /**
     * 生成全局通用參數(shù)
     *
     * @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("產(chǎn)品類型")
                .required(false)
                .in(ParameterType.HEADER)
                .build());
        return parameters;
    }

    /**
     * 生成通用響應(yīng)信息
     *
     * @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進(jìn)行測試

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. 查看效果圖

如何實(shí)現(xiàn)knife4j導(dǎo)出離線接口文檔

如何實(shí)現(xiàn)knife4j導(dǎo)出離線接口文檔

如何實(shí)現(xiàn)knife4j導(dǎo)出離線接口文檔

如何實(shí)現(xiàn)knife4j導(dǎo)出離線接口文檔

到此,相信大家對“如何實(shí)現(xiàn)knife4j導(dǎo)出離線接口文檔”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI