knife4j整合swagger3.0导出接口文档


   io.springfox
   springfox-boot-starter
   3.0.0


   com.github.xiaoymin
   knife4j-spring-boot-starter
   3.0.2

集成knife4j2.09的是没有导出world的功能,故升级3.0.2,注意启动类扫描,进可能范围大点

@SpringBootApplication
//@ComponentScan(basePackages = "com.xx.**.api")
@ComponentScan(basePackages = "com.xx.cloud.*")
public class DrmsSwaggerApplication {
   public static void main(String[] args) throws Exception {
      // http://10.0.2.99:9997/swagger-ui/index.html?url=/v3/api-docs
      SpringApplication.run(DrmsSwaggerApplication.class, args);
   }
}

package com.audaque.cloud.drms;

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;

/**
 * lgh
 */
@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.audaque.cloud.drms.portal.api"))
                .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", "", "[email protected]"))
                .version("1.0")
                .build();
    }

    /**
     * 生成全局通用参数
     *
     * @return
     */

    private List getGlobalRequestParameters() {
        List 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 getGlobalResponseMessage() {
        List responseList = new ArrayList<>();
        responseList.add(new ResponseBuilder().code("404").description("找不到资源").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("oauth2schema", authorizationScopeList, grantTypes);
//
//    }

//
//    private SecurityContext securityContext() {
//        return SecurityContext.builder()
//                .securityReferences(defaultAuth())
//                .forPaths(PathSelectors.ant("/v1/api/**"))
//                .build();
//    }

    private List defaultAuth() {

        final AuthorizationScope[] authorizationScopes = new AuthorizationScope[0];
        return Collections.singletonList(new SecurityReference("oauth2schema", authorizationScopes));
    }
}

@RestController
@Api(tags = "学生接口")//描述Controller的信息 现在的平台的api里面没有这个标签,导致导出的是类名
public class TestController {

Api标签要在controller加上,不然扫描出来就是类名

knife4j整合swagger3.0导出接口文档_第1张图片

你可能感兴趣的:(java)