Swagger2:无法自检类[springfox.documentation.swagger2.配置. Swagger2文档配置]

wlsrxk51  于 2023-02-04  发布在  Spring
关注(0)|答案(3)|浏览(573)

我试图在我的项目中使用swagger2,但得到以下错误。
“java.lang.非法状态异常:无法从类加载器[jdk.internal.loader.ClassLoaders$AppClassLoader@16f65612]自检类[springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration]”

//Spring Boot version: 2.1.9
//Swagger2: 2.9.2
//java: 11

package path.to.package;

import static springfox.documentation.builders.PathSelectors.regex;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

  @Bean
  public Docket OrganizationApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.basePackage("path.to.package"))
        .paths(regex("/api/.*"))
        .build();
  }

}

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
</dependency>

完整堆栈跟踪:

at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:507)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:404)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:389)
    at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:447)
    at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:738)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:679)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:647)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1518)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:513)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:483)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:604)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:596)
    at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1226)
    at org.springframework.boot.SpringApplication.getExitCodeFromMappedException(SpringApplication.java:866)
    at org.springframework.boot.SpringApplication.getExitCodeFromException(SpringApplication.java:854)
    at org.springframework.boot.SpringApplication.handleExitCode(SpringApplication.java:841)
    at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:792)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)
    at com.path.to.package.Application.main(Application.java:25)
Caused by: java.lang.NoClassDefFoundError: org/springframework/web/servlet/HandlerMapping
    at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167)
    at java.base/java.lang.Class.getDeclaredMethods(Class.java:2310)
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:489)
    ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.web.servlet.HandlerMapping
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 25 common frames omitted
jdgnovmf

jdgnovmf1#

java.lang.ClassNotFoundException:handlerMapping意味着您在pom.xml中缺少了一个依赖项,确切地说,您缺少了spring-webmvc依赖项。
当您使用Spring Boot时,只需将此依赖项添加到您的pom:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

仔细检查任何其他可能丢失的依赖项。如果您有任何问题,请在这里发布您的pom.xml,我们可以找到丢失的依赖项。

4c8rllxm

4c8rllxm2#

请尝试以下代码

@Configuration
public class SwaggerConfig implements WebMvcConfigurer {    
@Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/api/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
            registry.addResourceHandler("/api/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }

        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2).select()
                    .apis(RequestHandlerSelectors.basePackage("com.org.v1")).paths(PathSelectors.any())
                    .build().apiInfo(apiInfo());
        }

        private ApiInfo apiInfo() {
            return new ApiInfo("Service Name", "API Description", "API", "Terms of service",
                    new Contact("name", "webaddress", "email"), "License of API", "API license URL",
                    Collections.emptyList());
        }

Swagger依赖项已用于版本2.7.0

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>

@EnableSwagger2可以位于同一位置或主应用程序类

ppcbkaq5

ppcbkaq53#

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.1</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.user-registration</groupId>
<artifactId>registration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>registration</name>
<description>Registration</description>
<properties>
    <java.version>17</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.0</version>
    </dependency>

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

    <!-- 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    
    -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!--        <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity6</artifactId>
    </dependency>
-->

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- 
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    -->
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

相关问题