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
Spring Boot Whitelabel Error page (type=Not Found, status=404)
提问by sounobre
Good afternoon! I'm starting spring studies, I'm following a tutorial the same way, but it returns an error:
下午好!我正在开始春季学习,我正在以同样的方式学习教程,但它返回一个错误:
Folder structure:
文件夹结构:
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 @Controller
over 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 @Controller
along with @ResponseBody
annotation or you can directly use @RestController
over the Controller
class. It worked for me as I was getting the same error while creating SpringBoot project with RestFul webservices.
解决方案:如果您使用@Controller
的是控制器类,那么它将被视为 MVC 控制器类。但是如果你想要一个在 RESTFul web 服务中使用的特殊控制器,那么你可以@Controller
与@ResponseBody
注解一起使用,或者你可以直接@RestController
在Controller
类上使用。它对我有用,因为我在使用 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 的初学者,这个错误可能发生在任何人身上至少一次,原因有几个。
- 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
@SpringBootApplication
orextends SpringBootApplication
- You may miss
@RestController
annotation at top of the controller class. - Or can be missed the mapping annotation.(
@PostMapping("/save")
or@GetMapping("/id")
etc.) - 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.
- Application 类(例如:SpringAppApplication.java)应该在根级别,并且控制器类可以放在同一级别或子目录中。此应用程序类应使用
@SpringBootApplication
或表示extends SpringBootApplication
- 您可能会错过
@RestController
控制器类顶部的注释。 - 或者可能会错过映射注释。(
@PostMapping("/save")
或@GetMapping("/id")
等) - 最后只需检查您是否输入了正确的请求映射地址(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 {
.....
}