【问题标题】:Error message when trying to implement Swagger in my Java Spring API尝试在我的 Java Spring API 中实现 Swagger 时出现错误消息
【发布时间】:2020-05-15 17:01:26
【问题描述】:

在我使用 Spring 制作的 API 中添加 Swagger 后,我在运行时收到以下错误消息:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-15 13:51:39.631 ERROR 13804 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory.<init>(UnwrappingRepositoryInvokerFactory.java:57)

The following method did not exist:

    'org.springframework.plugin.core.PluginRegistry org.springframework.plugin.core.PluginRegistry.of(java.util.List)'

The method's class, org.springframework.plugin.core.PluginRegistry, is available from the following locations:

    jar:file:/home/gabriel/.m2/repository/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar!/org/springframework/plugin/core/PluginRegistry.class

It was loaded from the following location:

    file:/home/gabriel/.m2/repository/org/springframework/plugin/spring-plugin-core/1.2.0.RELEASE/spring-plugin-core-1.2.0.RELEASE.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.plugin.core.PluginRegistry


Process finished with exit code 1

我的 pom.xml 文件:

<?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>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tropicalia</groupId>
    <artifactId>users_api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>users_api</name>
    <description>1st users API with Spring</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

        <dependencies>

<dependency>
    <groupId>org.springframework.plugin</groupId>
    <artifactId>spring-plugin-core</artifactId>
    <version>1.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

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

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

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-browser</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.5</version>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.3.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.xmlunit</groupId>
            <artifactId>xmlunit-core</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.3.1.Final</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.8</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>


        </plugins>
    </build>

</project>

编码明智,我认为一切都很好。我几乎可以肯定这个问题的解决方案是在maven文件的依赖项中。

我尝试了一些我在网上找到的解决方案,但都没有奏效,如果有人能与我分享一些这方面的知识,我将不胜感激。

【问题讨论】:

  • 您可以尝试像这样修改&lt;dependency&gt; &lt;groupId&gt;org.springframework.plugin&lt;/groupId&gt; &lt;artifactId&gt;spring-plugin-core&lt;/artifactId&gt; &lt;version&gt;2.0.0.RELEASE&lt;/version&gt; &lt;/dependency&gt; 以覆盖旧版本的 swagger 并使用相同的 spring-boot 吗?

标签: java spring api maven swagger


【解决方案1】:

可以从您的 POM 文件中删除以下依赖项:

<dependency>
    <groupId>org.springframework.plugin</groupId>
    <artifactId>spring-plugin-core</artifactId>
    <version>1.2.0.RELEASE</version>
</dependency>

这个依赖已经是 Swagger 依赖的传递依赖(编译范围)。 单独添加此依赖项会在运行时与 spring-boot-starter-data-rest 依赖项产生冲突。通过从您的 POM 文件中删除它,您允许 Maven 选择正确的版本(这将是与 Spring Boot 兼容的版本,因为它是最新的)。目前版本强制为1.2.0.RELEASE,而Spring Boot可能需要2.0.0.RELEASE

Swagger 只需要您的 POM 文件中的以下依赖项:

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

如果您的应用程序确实需要 spring-plugin-core 依赖项,您可以修改版本而不是删除它。将其添加到 Swagger 依赖项中也是正确的做法。

【讨论】:

  • 感谢您的回答!我理解您所说的并做到了,但运行该应用程序的成功率仍然为零。我现在认为该错误在此评论中有一些根源:Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory;嵌套异常是 org.hibernate.MappingException:无法获取 org.hibernate.persister.entity.SingleTableEntityPersister 的构造函数
  • 这个错误看起来与 Swagger 无关。如果删除 Swagger 依赖并重新构建,是否也会出现同样的错误?
【解决方案2】:

所以我找到了问题的解决方案,这完全是因为我在我的 api 中使用的软件版本之间不兼容。

我将 spring 版本更改为 2.3.0 并在我的 pom 文件中添加了以下依赖项:

<dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.23.1-GA</version>
    </dependency>

信用: https://github.com/spring-projects/spring-boot/issues/14610

【讨论】:

    猜你喜欢
    • 2019-12-30
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多