繁体   English   中英

java.lang.NoClassDefFoundError:org/springframework/web/util/UriTemplateHandler

[英]java.lang.NoClassDefFoundError:org/springframework/web/util/UriTemplateHandler

我有一个使用 Spring Initializer 创建的基本 SpringBoot 应用程序。 当我尝试启动 SpringBoot 服务器(使用 maven 插件 (spring-boot:start))时,出现以下错误。

java.lang.NoClassDefFoundError: org/springframework/web/util/UriTemplateHandler at java.lang.Class.getDeclaredConstructors0(Native Method) ~[na:1.8.0_60] at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) ~[na:1.8.0_60] 在 java.lang.Class.getDeclaredConstructors(Class.java:2020) ~[na:1.8.0_60] 在 org.springframework.boot.context.properties.ConfigurationPropertiesBindConstructorProvider.findConstructorBindingAnnotatedConstructor(ConfigurationProperties.javaBindConstructor(ConfigurationProperties.javaConstructor) 62) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.boot.context.properties.ConfigurationPropertiesBindConstructorProvider.getBindConstructor(ConfigurationPropertiesBindConstructorProvider.java:48) ~[spring-boot-2.2 .4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.boot.context.properties.ConfigurationPropertiesBean$BindMethod.forType(ConfigurationPropertiesBean.java:311) ~[spring-boot-2.2.4.RELEASE.jar: 2.2.4.RELEASE] 在 org.springframework.boot.context.properties.Configu rationPropertiesBeanDefinitionValidator.validate(ConfigurationPropertiesBeanDefinitionValidator.java:63) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.postProcessBeanFactory(ConfigurationProperties.BeanDefinitionValidator. ) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] 在 org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286) ~[spring-context-5.2.3. RELEASE.jar:5.2.3.RELEASE] 在 org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:174) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]

这是我的 pom 文件。

    <?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.4.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.cgtm</groupId>
    <artifactId>validator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>validator</name>
    <description>Validator</description>

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

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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

</project>

应用程序.java文件

 package com.cgtm.validator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ValidatorApplication {

    public static void main(String[] args) {
        SpringApplication.run(ValidatorApplication.class, args);
    }

}

Spring boot 的版本是 2.2.4,它需要 Spring 5(基本上它带有预定义的 spring

另一方面,您使用的是“硬编码”spring web 4.1.4,这是错误的:

 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.1.6.RELEASE</version>
 </dependency>

你应该使用而不是:

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

确保您的路径上没有任何 spring 4.x 依赖项(通过运行mvn dependency:tree

此外,您不需要放置:

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

最后一件事,您正在使用 lombok,这当然很好(我的意思是 spring 可以很好地与 lombok 一起使用),但是尽管provided范围,spring boot maven 插件仍会将其打包到工件中。 如果您希望将其从最终工件中排除,则必须配置 spring boot maven 插件本身才能这样做。

阅读此 SO 线程以获取更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM