首页 > 解决方案 > Spring Boot RequestMapping 在 Linux Fedora 26 上不起作用

问题描述

我最近开始使用 Linux Fedora 26 并想启动一个 Spring Boot 应用程序,但它无法识别带有 Thymeleaf 的 @Controller 和 @RestController 注释。我的 HomeController 类的映射看不到我的 Templates 文件夹,我得到一个 Whiteable 错误页面/404。我已经开始在我的旧 Windows 机器上构建一个类似的项目(https://github.com/alexanderphoen1x/trainingdiary),看起来工作正常,但是当我尝试运行我的旧项目时,我从 github 克隆了它,它给了我一个例外:

Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource  

[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed

我认为这可能与Linux有关?

项目结构:

1. greenenergy
    -src/main/java
        +com.greenenergy
            *GreenEnergyApplication.java
        +com.greenenergy.controller
            *HomeController.java
    -src/main/resources
        +static
        +templates
            *stories.html
        +application.properties
(...)
    -pom.xml

我的代码:GreenEnergyApplication.java:

package com.greenenergy;

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

@SpringBootApplication
public class GreenEnergyApplication {

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

HomeController.java:

package com.greenergy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {
    @RequestMapping("/")
    public String stories() {
        return "stories";
    }
}

故事.html:

<!DOCTYPE html>
<html>
<title></title>
<head></head>
<body>Hey Alex</body>
</html>

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.greenenergy</groupId>
    <artifactId>greenenergy</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Green energy project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

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

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

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

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


</project>

安慰:

2018-10-22 17:31:32.580  INFO 5091 --- [  restartedMain] com.greenenergy.GreenEnergyApplication   : Starting GreenEnergyApplication on localhost.localdomain with PID 5091 (/home/alexanderp/Projects/greenenergy/greenenergy/target/classes started by alexanderp in /home/alexanderp/Projects/greenenergy/greenenergy)
2018-10-22 17:31:32.581  INFO 5091 --- [  restartedMain] com.greenenergy.GreenEnergyApplication   : No active profile set, falling back to default profiles: default
2018-10-22 17:31:32.650  INFO 5091 --- [  restartedMain] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@76dffce0: startup date [Mon Oct 22 17:31:32 CEST 2018]; root of context hierarchy
2018-10-22 17:31:34.411  INFO 5091 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-10-22 17:31:34.438  INFO 5091 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-10-22 17:31:34.439  INFO 5091 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-10-22 17:31:34.446  INFO 5091 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : Loaded APR based Apache Tomcat Native library [1.2.16] using APR version [1.6.3].
2018-10-22 17:31:34.447  INFO 5091 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2018-10-22 17:31:34.447  INFO 5091 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2018-10-22 17:31:34.450  INFO 5091 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.0h-fips  27 Mar 2018]
2018-10-22 17:31:34.546  INFO 5091 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-10-22 17:31:34.546  INFO 5091 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1900 ms
2018-10-22 17:31:34.619  INFO 5091 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-10-22 17:31:34.623  INFO 5091 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-10-22 17:31:34.624  INFO 5091 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-10-22 17:31:34.624  INFO 5091 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-10-22 17:31:34.624  INFO 5091 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-10-22 17:31:34.785  INFO 5091 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-22 17:31:35.027  INFO 5091 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@76dffce0: startup date [Mon Oct 22 17:31:32 CEST 2018]; root of context hierarchy
2018-10-22 17:31:35.117  INFO 5091 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-10-22 17:31:35.119  INFO 5091 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-10-22 17:31:35.177  INFO 5091 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-22 17:31:35.177  INFO 5091 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-22 17:31:35.425  INFO 5091 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2018-10-22 17:31:35.453  INFO 5091 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-10-22 17:31:35.482  INFO 5091 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-10-22 17:31:35.487  INFO 5091 --- [  restartedMain] com.greenenergy.GreenEnergyApplication   : Started GreenEnergyApplication in 3.246 seconds (JVM running for 4.164)
2018-10-22 17:31:40.437  INFO 5091 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-10-22 17:31:40.437  INFO 5091 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-10-22 17:31:40.450  INFO 5091 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 12 ms
2018-10-22 17:31:48.845  INFO 5091 --- [       Thread-7] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@76dffce0: startup date [Mon Oct 22 17:31:32 CEST 2018]; root of context hierarchy
2018-10-22 17:31:48.848  INFO 5091 --- [       Thread-7] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

标签: javalinuxspringmavenspring-boot

解决方案


Maven 依赖项可能在下载过程中损坏。注释掉 POM 中的 Thymeleaf 并保存项目。手动删除驱动器上的 Thymeleaf 文件。在 POM 中放回 Thymeleaf 依赖项并保存项目。


推荐阅读