gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:04:27 29 4
gpt4 key购买 nike

我有一个使用 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] at java.lang.Class.getDeclaredConstructors(Class.java:2020) ~[na:1.8.0_60] at org.springframework.boot.context.properties.ConfigurationPropertiesBindConstructorProvider.findConstructorBindingAnnotatedConstructor(ConfigurationPropertiesBindConstructorProvider.java:62) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] at org.springframework.boot.context.properties.ConfigurationPropertiesBindConstructorProvider.getBindConstructor(ConfigurationPropertiesBindConstructorProvider.java:48) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] at org.springframework.boot.context.properties.ConfigurationPropertiesBean$BindMethod.forType(ConfigurationPropertiesBean.java:311) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.validate(ConfigurationPropertiesBeanDefinitionValidator.java:63) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.postProcessBeanFactory(ConfigurationPropertiesBeanDefinitionValidator.java:45) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE] at 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>

Application.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 配合使用),但是 spring boot maven 插件仍然会将其打包到工件中,尽管提供了 范围。如果您希望将其从最终工件中排除,则必须配置 Spring Boot Maven 插件本身才能执行此操作。

阅读this SO thread了解更多信息

关于java.lang.NoClassDefFoundError :org/springframework/web/util/UriTemplateHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59924733/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com