spring 此应用程序没有明确的 /error 映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31134333/
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
This application has no explicit mapping for /error
提问by Deng Steve
I used maven to do the tutorial https://spring.io/guides/gs/uploading-files/
All the codes I used was copied.
我用maven做教程https://spring.io/guides/gs/uploading-files/
我用的所有代码都是复制的。
The Application can run, but I get the error:
应用程序可以运行,但出现错误:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue Jun 30 17:24:02 CST 2015 There was an unexpected error (type=Not Found, status=404). No message available
白标错误页面 此应用程序没有明确的 /error 映射,因此您将其视为后备。2015 年 6 月 30 日星期二 17:24:02 CST 出现意外错误(类型 = 未找到,状态 = 404)。没有可用的消息
How can I fix it?
我该如何解决?
回答by vignesh Subash
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 只会扫描你的主类包下面的类。
com
+- APP
+- Application.java <--- your main class should be here, above your controller classes
|
+- model
| +- user.java
+- controller
+- UserController.java
回答by musibs
When we create a Spring boot application we annotate it with @SpringBootApplication
annotation. This annotation 'wraps up' many other necessary annotations for the application to work. One such annotation is @ComponentScan
annotation. This annotation tells Spring to look for Spring components and configure the application to run.
当我们创建一个 Spring boot 应用程序时,我们用注释对其进行@SpringBootApplication
注释。此注释“包含”了应用程序运行所需的许多其他注释。一种这样的注释是@ComponentScan
注释。该注解告诉 Spring 查找 Spring 组件并配置要运行的应用程序。
Your application class needs to be top of your package hierarchy, so that Spring can scan sub-packages and find out the other required components.
您的应用程序类需要位于包层次结构的顶部,以便 Spring 可以扫描子包并找出其他所需的组件。
package com.test.spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Below code snippet worksas the controller package is under com.test.spring.boot
package
下面的代码片段工作,因为控制器包在com.test.spring.boot
包下
package com.test.spring.boot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@RequestMapping("/")
public String home(){
return "Hello World!";
}
}
Below code snippet does NOT Workas the controller package is NOT under com.test.spring.boot
package
下面的代码片段 不起作用,因为控制器包不在com.test.spring.boot
包下
package com.test.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
@RequestMapping("/")
public String home(){
return "Hello World!";
}
}
From Spring Boot documentation:
从 Spring Boot 文档:
Many Spring Boot developers always have their main class annotated with
@Configuration
,@EnableAutoConfiguration
and@ComponentScan
. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient@SpringBootApplication
alternative.The
@SpringBootApplication
annotation is equivalent to using@Configuration
,@EnableAutoConfiguration
and@ComponentScan
with their default attributes
许多 Spring Boot 开发人员总是用
@Configuration
,@EnableAutoConfiguration
和注释他们的主类@ComponentScan
。由于这些注释经常一起使用(特别是如果您遵循上述最佳实践),Spring Boot 提供了一个方便的@SpringBootApplication
替代方案。该
@SpringBootApplication
注解相当于使用@Configuration
,@EnableAutoConfiguration
并@ComponentScan
与他们的默认属性
回答by owaism
You can solve this by adding an ErrorController
in your application. You can have the error controller return a view that you need.
您可以通过ErrorController
在应用程序中添加 来解决此问题。您可以让错误控制器返回您需要的视图。
Error Controller in my application looks like below:
我的应用程序中的错误控制器如下所示:
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* Basic Controller which is called for unhandled errors
*/
@Controller
public class AppErrorController implements ErrorController{
/**
* Error Attributes in the Application
*/
private ErrorAttributes errorAttributes;
private final static String ERROR_PATH = "/error";
/**
* Controller for the Error Controller
* @param errorAttributes
*/
public AppErrorController(ErrorAttributes errorAttributes) {
this.errorAttributes = errorAttributes;
}
/**
* Supports the HTML Error View
* @param request
* @return
*/
@RequestMapping(value = ERROR_PATH, produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request) {
return new ModelAndView("/errors/error", getErrorAttributes(request, false));
}
/**
* Supports other formats like JSON, XML
* @param request
* @return
*/
@RequestMapping(value = ERROR_PATH)
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
/**
* Returns the path of the error page.
*
* @return the error path
*/
@Override
public String getErrorPath() {
return ERROR_PATH;
}
private boolean getTraceParameter(HttpServletRequest request) {
String parameter = request.getParameter("trace");
if (parameter == null) {
return false;
}
return !"false".equals(parameter.toLowerCase());
}
private Map<String, Object> getErrorAttributes(HttpServletRequest request,
boolean includeStackTrace) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return this.errorAttributes.getErrorAttributes(requestAttributes,
includeStackTrace);
}
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
if (statusCode != null) {
try {
return HttpStatus.valueOf(statusCode);
}
catch (Exception ex) {
}
}
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
The above class is based on Springs BasicErrorControllerclass.
上面的类基于 Springs BasicErrorController类。
You can instantiate the above ErrorController
like this in a @Configuration
file:
您可以ErrorController
在@Configuration
文件中像这样实例化上述内容:
@Autowired
private ErrorAttributes errorAttributes;
@Bean
public AppErrorController appErrorController(){return new AppErrorController(errorAttributes);}
You can choose override the default ErrorAttributes
by implementing ErrorAttributes. But in most cases the DefaultErrorAttributesshould suffice.
您可以ErrorAttributes
通过实现ErrorAttributes来选择覆盖默认值。但在大多数情况下DefaultErrorAttributes就足够了。
回答by mykey
In my case the controller class was annotated with @Controller
. Changing that to @RestController
resolved the problem.
Basically @RestController
is @Controller + @ResponseBody
So either use @RestController
, or @Controller
with @ResponseBody
annotation with each method.
在我的例子中,控制器类用@Controller
. 更改它以@RestController
解决问题。基本上@RestController
是@Controller + @ResponseBody
所以要么使用@RestController
,要么对每个方法@Controller
进行@ResponseBody
注释。
Some useful notes here : https://www.genuitec.com/spring-frameworkrestcontroller-vs-controller/
这里有一些有用的说明:https: //www.genuitec.com/spring-frameworkrestcontroller-vs-controller/
回答by Mina Fawzy
in my case it because of package position , meaning package of controller must be above main class package
在我的情况下是因为包位置,这意味着控制器包必须在主类包之上
if my main class package is package co.companyname.spring.tutorial;
any controller package should package co.companyname.spring.tutorial.WHAT_EVER_HERE;
如果我的主类包是package co.companyname.spring.tutorial;
任何控制器包应该package co.companyname.spring.tutorial.WHAT_EVER_HERE;
package co.companyname.spring.tutorial; // package for main class
@SpringBootApplication
public class FirstProjectApplication {
public static void main(String[] args) {
SpringApplication.run(FirstProjectApplication.class, args);
}
}
package co.companyname.spring.tutorial.controllers; // package for controllers
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello, world";
}}
after finish coding press boot dashboard
完成编码后按启动仪表板
one last thing to make sure your controller is mapping or not just console you should see somehting smilliar
最后一件事是确保您的控制器正在映射或不仅仅是控制台,您应该看到一些类似的东西
Mapped "{[/hello]}" onto public java.lang.String co.companyname.spring.tutorial.controllers.HelloController.hello()
happy coding
快乐编码
回答by prabhat kumar
This happens when an explicit error page is not defined. To define an error page, create a mapping of /error with a view. e.g. the below code maps to a string value being returned in case of an error.
当未定义显式错误页面时会发生这种情况。要定义错误页面,请使用视图创建 /error 的映射。例如,下面的代码映射到出现错误时返回的字符串值。
package com.rumango.controller;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class IndexController implements ErrorController{
private final static String PATH = "/error";
@Override
@RequestMapping(PATH)
@ResponseBody
public String getErrorPath() {
// TODO Auto-generated method stub
return "No Mapping Found";
}
}
回答by Sree
Try adding the dependency.
尝试添加依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
回答by Ekene Oguikpu
I added this dependency and it solved my problem.
我添加了这个依赖项,它解决了我的问题。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
回答by Semih Erkaraca
I am developing Spring Boot application for a few weeks.. And i was gettig same error like below;
我正在开发 Spring Boot 应用程序几个星期。我得到了与下面相同的错误;
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Thu Jan 18 14:12:11 AST 2018 There was an unexpected error (type=Not Found, status=404). No message available
白标错误页面 此应用程序没有明确的 /error 映射,因此您将其视为后备。星期四 1 月 18 日 14:12:11 AST 2018 出现意外错误(类型 = 未找到,状态 = 404)。没有可用的消息
When i get this error massage i realized my controller or rest controller class is note defined in my project. I mean our all controller packages aren't same package with main class which include @SpringBootApplication annotation..I mean you need to add you controller package's name to @ComponentScan annotation to your main class which is inludes @SpringBootApplication annotation.If you write codes of below your problem will be solving... Most important thing is you have to add your all controller's package to @ComponentScan annotation like i did in the below
当我收到此错误消息时,我意识到我的控制器或其余控制器类是在我的项目中定义的注释。我的意思是我们所有的控制器包不是同一个包包含主类,其中包括@SpringBootApplication注解..我的意思是,你需要你控制器封装的名字添加到@ComponentScan注释这是inludes @SpringBootApplication annotation.If你写代码的主类下面的问题将得到解决......最重要的是你必须像我在下面所做的那样将所有控制器的包添加到@ComponentScan 注释中
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({ "com.controller.package1, com.controller.package2, com.controller.package3, com.controller.packageN", "controller", "service" } // If our Controller class or Service class is not in the same packages we have //to add packages's name like this...directory(package) with main class
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
I hope this codes are going to help someone...
我希望这些代码可以帮助某人...
If you find another way to solve this error or you have some suggestions for me, please write to comments... thanks...
如果您找到解决此错误的另一种方法,或者您对我有一些建议,请写信给评论...谢谢...
回答by Harika
In the main class, after the configuration "@SpringBootApplication", adding "@ComponentScan" without having any arguments, worked for me !!!
在主类中,在配置“@SpringBootApplication”之后,不带任何参数添加“@ComponentScan”,对我有用!!!
Main Class :
主要班级:
@SpringBootApplication
@ComponentScan
public class CommentStoreApplication {
public static void main(String[] args) {
SpringApplication.run(CommentStoreApplication.class, args);
}
}
RestController Class :
休息控制器类:
@RestController
public class CommentStoreApp {
@RequestMapping("/")
public String hello() {
return "Hello World!";
}
}
P.S: Don't miss to run mvn clean and mvn install commands, before launching the application
PS:在启动应用程序之前,不要错过运行 mvn clean 和 mvn install 命令