【问题标题】:Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.method.HandlerMethod更正应用程序的类路径,使其包含一个兼容的 org.springframework.web.method.HandlerMethod 版本
【发布时间】:2021-11-07 02:22:15
【问题描述】:

我已经开始编写简单的 Spring Boot 应用程序,但遇到了一些问题。我有控制器 home.html 页面,显示 Hello Home Page。

这是错误:

 Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:    org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.createHandlerMethod(AbstractHandlerMethodMapping.java:341)
The following method did not exist:
    'void org.springframework.web.method.HandlerMethod.<init>(java.lang.String, org.springframework.beans.factory.BeanFactory, org.springframework.context.MessageSource, java.lang.reflect.Method)'

Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.method.HandlerMethod
Process finished with exit code 0

Pom.xml

只有 pom.xml 中的依赖代码

<project>

    <dependencies>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.6</version>
        </dependency>
    </dependencies>


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

</project>

主控制器

控制器包中的MainController代码

@Controller
public class MainController {

    @GetMapping("/")
    public String greeting(Model model) {
        model.addAttribute("title", "Home Page");
        return "home";
    }

}

home.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title th:text="${title}"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${title} + '!'" />
</body>
</html>

百万应用

默认方法来自

@SpringBootApplication
public class MillionApplication {

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

}

有人遇到过类似的问题吗? 有人可以帮助我吗,因为我尝试了很多东西但没有任何效果

【问题讨论】:

  • 你使用哪个 spring-boot 版本?
  • 移除 spring-web 依赖,spring-boot-starter-web 已经包含了正确的版本。

标签: java html spring spring-boot spring-mvc


【解决方案1】:

您有spring-boot-starter-web 依赖项,其中spring-web 作为传递依赖项,因此您不需要在pom.xml 中明确定义spring-web。此外,您应该依赖 Spring Boot Starters 的传递依赖项和版本,而不是自己定义版本(就像您对 spring-web 所做的那样)。 话虽如此,请删除以下依赖项:

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

【讨论】:

  • 没用
  • 必须,错误消息非常清楚:“更正应用程序的类路径,使其包含一个兼容的 org.springframework.web.method.HandlerMethod 版本”。请使用mvn clean package重新编译代码。
猜你喜欢
  • 2022-01-01
  • 2021-06-29
  • 2019-08-11
  • 2021-07-08
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多