.OpenApiExtensionResolver' that could not be found.

发布于 2022-03-07 16:56:48 字数 9857 浏览 14 评论 2

启动失败,错误信息:,

Parameter 0 of constructor in com.simshine.config.SwaggerConfiguration required a bean of type 'com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver' that could not be found.

Action:

Consider defining a bean of type 'com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver' in your configuration.

 

配置文件:

import com.fasterxml.classmate.TypeResolver;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import com.simshine.model.configuration.SwaggerModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static springfox.documentation.schema.AlternateTypeRules.newRule;

@Configuration
//@EnableSwagger2WebMvc
@EnableSwagger2
@EnableKnife4j
/**
 * @ClassName Swagger2Config
 * @Author htz
 * @Description swagger2配置
 * @Data 2019-04-09
 * @Version 1.0.0
 **/
public class SwaggerConfiguration {
    private final OpenApiExtensionResolver openApiExtensionResolver;

    @Autowired
    public SwaggerConfiguration(OpenApiExtensionResolver openApiExtensionResolver) {
        this.openApiExtensionResolver = openApiExtensionResolver;
    }

    @Autowired
    private TypeResolver typeResolver;
    @Autowired
    SwaggerModel swaggerModel;
    private static final String FLAG_TOKEN = "flagToken";
    private static final String UID = "uid";

    /**
     * 全局设置Content Type,默认是application/json
     *  此处我给的空,避免文档上给默认值
     * 如果想只针对某个方法,则注释掉改语句,在特定的方法加上下面信息
     * @ApiOperation(consumes="application/x-www-form-urlencoded")
     */
    private static final HashSet<String> CONSUMES = new HashSet<>();
    static {
        CONSUMES.add("   ");
    }


    @Bean
    public Docket petInnerApi() {
        return this.getDocket("face-app-api","com.simshine.controller.faceapi.app");
    }

    private Docket getDocket(String groupName,String basePackage){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(groupName)
                .enable(swaggerModel.isEnable())
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build()
                .pathMapping("/")
                .directModelSubstitute(LocalDate.class, String.class)
                .genericModelSubstitutes(ResponseEntity.class)
                .alternateTypeRules(
                        newRule(typeResolver.resolve(DeferredResult.class,
                                typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
                                typeResolver.resolve(WildcardType.class)))
                .useDefaultResponseMessages(false)
                .securitySchemes(securitySchemes())
                .consumes(SwaggerConfiguration.CONSUMES)
                .securityContexts(securityContexts())
                .extensions(openApiExtensionResolver.buildSettingExtensions());
    }









    private ApiInfo apiInfo() {
        Contact contact = new Contact("htz","http://.qq.com/r/",");
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("响应码")
                .version("2.0.0")
                .contact(contact)
                .termsOfServiceUrl("www.luuka.fun")
                .build();
    }

    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> apiKeys = new ArrayList<>();
        apiKeys.add(new ApiKey(FLAG_TOKEN, "token", "header"));
        apiKeys.add(new ApiKey(UID,"UUid","header"));
        return apiKeys;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder()
                .securityReferences(defaultAuth1())
                .forPaths(PathSelectors.regex("^(?!auth).*$")).build());
        return securityContexts;
    }

    private List<SecurityReference> defaultAuth1() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference(FLAG_TOKEN, authorizationScopes));
        securityReferences.add(new SecurityReference(UID, authorizationScopes));
        return securityReferences;
    }

    @Bean
    UiConfiguration uiConfig() {
        return UiConfigurationBuilder.builder()
                .deepLinking(true)
                .displayOperationId(false)
                .defaultModelsExpandDepth(1)
                .defaultModelExpandDepth(1)
                .defaultModelRendering(ModelRendering.EXAMPLE)
                .displayRequestDuration(true)
                .docExpansion(DocExpansion.NONE)
                .filter(false)
                .maxDisplayedTags(null)
                .operationsSorter(OperationsSorter.ALPHA)
                .showExtensions(false)
                .tagsSorter(TagsSorter.ALPHA)
                .supportedSubmitMethods(UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS)
                .validatorUrl(null)
                .build();
    }



相关jar 版本

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>3.0.0</version>
</dependency>


<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>3.0.0</version>
</dependency>


<!--整合Knife4j-->
<!-- 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>
}

 

 

 

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永不分离 2022-03-11 00:32:22

yml配置文件中配置: knife4j.enable=true

@Import(BeanValidatorPluginsConfiguration.class)
无人问我粥可暖 2022-03-10 19:21:34

OpenApiExtensionResolver类在Spring中没有该Bean(未注册)。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文