【问题标题】:Spring Boot Whitelabel Error page (type=Not Found, status=404)Spring Boot Whitelabel 错误页面(类型=未找到,状态=404)
【发布时间】:2018-08-08 14:38:23
【问题描述】:

下午好! 我正在开始春季学习,我正在以相同的方式学习教程,但它返回错误:

文件夹结构:

奇怪的是: 如果我将“EventoController.java”插入 br.com.SpringApp.SpringApp,它可以正常工作。

package br.com.SpringApp.SpringApp;

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

@SpringBootApplication
public class SpringAppApplication {

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

.

package br.com.SpringApp.controller;

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

@Controller
public class EventoController {

    @RequestMapping("/cadastroEvento")
    public String form() {      
        return "evento/formEvento"; 
    }

}

根据要求,我正在添加 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>br.com.SpringApp</groupId>
    <artifactId>SpringApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringApp</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.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-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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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


   </project>

谁能告诉我哪里错了?

【问题讨论】:

  • 你能发布你的 pom.xml 吗?
  • 在下面提到的层次结构包 br.com.SpringApp.SpringApp.contorller 中声明你的控制器;就像您需要为模型声明一样,它也会解决您的问题
  • 根据要求,我正在添加 pom.xml

标签: java eclipse spring-boot thymeleaf


【解决方案1】:

作为 SpringBoot 的初学者,任何人都可能至少发生一次此错误,原因有几个。

  1. 应用程序类(例如:SpringAppApplication.java)应该在根目录下,控制器类可以放在同一目录下,也可以放在子目录下。此应用程序类应使用@SpringBootApplicationextends SpringBootApplication 表示
  2. 您可能会错过控制器类顶部的@RestController 注释。
  3. 或者可能会错过映射注释。(@PostMapping("/save")@GetMapping("/id") 等)
  4. 最后只需检查您是否输入了正确的请求映射地址 (URL),您可能会错过斜杠或 @RequestMapping("/customer")(如果您的控制器类中提供注释)或 URL 中的拼写错误。

【讨论】:

    【解决方案2】:

    解决方案: 如果您在 Controller 类上使用 @Controller,那么它将被视为 MVC 控制器类。但是如果你想在 RESTFul web 服务中使用一个特殊的控制器,那么你可以使用 @Controller@ResponseBody 注释,或者你可以直接在 Controller 类上使用 @RestController。它对我有用,因为我在使用 RestFul webservices 创建 SpringBoot 项目时遇到了同样的错误。

    package br.com.SpringApp.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class EventoController {
    
        @RequestMapping("/cadastroEvento")
        @ResponseBody
        public String form() {      
            return "evento/formEvento"; 
        }
    
    }
    

    或:

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RestController
    public class EventoController {
    
        @RequestMapping("/cadastroEvento")
        public String form() {      
            return "evento/formEvento"; 
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      正如 Deepak 所回答的,主类应该位于其他包之上的根包中。 但是如果你不想这样做,你可以使用:

      @SpringBootApplication(scanBasePackages = {"com.other.packages","com.other"})
      public class SpringAppApplication {
      .....
      }
      

      【讨论】:

        【解决方案4】:

        确保您的主类位于其他类之上的根包中。

        当您运行 Spring Boot 应用程序时(即使用 @SpringBootApplication 注解的类),Spring 将仅扫描您的主类包下的类。

        所以你的声明是这样的

        package br.com.SpringApp.SpringApp; 在这个主类中,即 SpringAppApplication

        package br.com.SpringApp.SpringApp.controller; 您的控制器的名称,即 EventoController 和 indexControllers

        package br.com.SpringApp.SpringApp.model; 您的模型名称,即 Evento

        之后 清理您的项目并重新运行 Spring Boot 应用程序;

        【讨论】:

        • 谢谢!问题正是这个......对不起这个愚蠢的怀疑......
        【解决方案5】:

        验证您的 pom.xml 中是否具有正确的 thymeleaf 依赖项:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-10-30
          • 2017-10-06
          • 2019-04-02
          • 2018-07-22
          • 2016-07-28
          • 2021-10-13
          • 2021-09-24
          • 2017-06-03
          相关资源
          最近更新 更多