eclipse Spring Boot Whitelabel 错误页面(类型=未找到,状态=404)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49034254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:32:29  来源:igfitidea点击:

Spring Boot Whitelabel Error page (type=Not Found, status=404)

javaeclipsespring-bootthymeleaf

提问by sounobre

Good afternoon! I'm starting spring studies, I'm following a tutorial the same way, but it returns an error:

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

enter image description here

在此处输入图片说明

Folder structure:

文件夹结构:

enter image description here

在此处输入图片说明

The strange thing is: If I insert the "EventoController.java" into br.com.SpringApp.SpringApp, it works correctly.

奇怪的是:如果我将“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"; 
    }

}

As requested, I'm adding pom.xml

根据要求,我正在添加 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>

Can someone tell me where I'm wrong, please?

有人能告诉我我错在哪里吗?

回答by Dipak Thoke

Make sure that your main class is in a root package above other classes.

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

When you run a Spring Boot Application, (i.e. a class annotated with @SpringBootApplication), Spring will only scan the classes below your main class package.

当你运行一个 Spring Boot 应用程序(即一个用@SpringBootApplication 注释的类)时,Spring 只会扫描你的主类包下面的类。

So your declaration goes like this

所以你的声明是这样的

package br.com.SpringApp.SpringApp;inside this main class i.e SpringAppApplication

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

package br.com.SpringApp.SpringApp.controller;name of your controllers i.e EventoController & indexControllers

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

package br.com.SpringApp.SpringApp.model;name of your models i.e Evento

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

After This clean your project and re-run spring boot application;

在此之后清理您的项目并重新运行 spring boot 应用程序;

回答by senape

verify that you have the correct thymeleaf dependency within your pom.xml:

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

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

回答by Mitali

Solution: If you are using @Controllerover the Controller class then it will be treated as a MVC controller class. But if you want a special controller used in RESTFul web services then you to use @Controlleralong with @ResponseBodyannotation or you can directly use @RestControllerover the Controllerclass. It worked for me as I was getting the same error while creating SpringBoot project with RestFul webservices.

解决方案:如果您使用@Controller的是控制器类,那么它将被视为 MVC 控制器类。但是如果你想要一个在 RESTFul web 服务中使用的特殊控制器,那么你可以@Controller@ResponseBody注解一起使用,或者你可以直接@RestControllerController类上使用。它对我有用,因为我在使用 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"; 
    }

}

or:

或者:

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

@RestController
public class EventoController {

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

}

回答by Ruchira Supipi

As a beginner to the SpringBoot, this error can be happened to anyone at least one time, because for a few reasons.

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

  1. The Application class (eg: SpringAppApplication.java) should be at the root level, as well as the controller classes can be placed under the same level or maybe in a subdirectory. This application class should be denoted using @SpringBootApplicationor extends SpringBootApplication
  2. You may miss @RestControllerannotation at top of the controller class.
  3. Or can be missed the mapping annotation.( @PostMapping("/save")or @GetMapping("/id")etc.)
  4. finally just check whether you are entering the correct request mapping address(URL) you may miss a slash or @RequestMapping("/customer")(if the annotation available in your controller class) or spellings error in the URL.
  1. Application 类(例如:SpringAppApplication.java)应该在根级别,并且控制器类可以放在同一级别或子目录中。此应用程序类应使用@SpringBootApplication或表示extends SpringBootApplication
  2. 您可能会错过@RestController控制器类顶部的注释。
  3. 或者可能会错过映射注释。(@PostMapping("/save")@GetMapping("/id")等)
  4. 最后只需检查您是否输入了正确的请求映射地址(URL),您可能会错过斜线或@RequestMapping("/customer")(如果您的控制器类中有可用的注释)或 URL 中的拼写错误。

回答by JasonD

As answered by Deepak, main class should be in a root package above other packages. But if you don't want to do this, you can use:

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

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