为啥我的 Spring Boot 应用程序出现异常?
Posted
技术标签:
【中文标题】为啥我的 Spring Boot 应用程序出现异常?【英文标题】:Why am I getting exception in my Spring Boot Application?为什么我的 Spring Boot 应用程序出现异常? 【发布时间】:2020-06-15 05:11:19 【问题描述】:我的 Spring Boot 应用程序,
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication
public static void main(String[] args)
SpringApplication.run(DemoApplication.class, args);
在获得 Whitelabel 错误页面后,我已将 /error
映射到我的一个控制器中,
@RestController
public class ResourceController
@RequestMapping("/home")
String home()
return "Hello, Welcome!";
@RequestMapping("/error")
String error()
return "Error occurred!";
当我映射/error
时,我遇到了异常,
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'basicErrorController' method
org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
to /error: There is already 'resourceController' bean method
com.example.demo.controller.ResourceController#error() mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
at com.example.demo.DemoApplication.main(DemoApplication.java:10) [classes/:na]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'basicErrorController' method
org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
to /error: There is already 'resourceController' bean method
更新:
遵循 User9123 的解决方案,但我仍然在页面下方,
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Mar 03 03:27:22 BDT 2020
There was an unexpected error (type=None, status=999).
No message available
【问题讨论】:
【参考方案1】:Spring 告诉您存在一个模棱两可的映射。由于com.example.demo.controller.ResourceController
和org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController
都具有相同的请求映射/error
。
因此,您不能将/error
保留在 ResourceController 类中。
您要么添加基本请求映射路径,要么更改特定 API 上的请求映射路径,这会导致模棱两可的情况。
例子:
第一种方式:在控制器上添加基本映射路径,消除歧义。
@RestController
@RequestMapping("/resource")
public class ResourceController
@RequestMapping("/home")
String home()
return "Hello, Welcome!";
@RequestMapping("/error")
String error()
return "Error occurred!";
注意:在这种情况下,所有 API 的路径都将以 <your url>/resource/<method request mapping path>
开头
或
第二种方式:更改导致不明确情况的特定 API 上的请求映射路径。
例如:
@RestController
public class ResourceController
@RequestMapping("/home")
String home()
return "Hello, Welcome!";
@RequestMapping("/resource/error")
String error()
return "Error occurred!";
注意:在这种情况下,您必须使用<your url>/resource/error
来调用错误API。
在我看来,我会建议你采用第一种方法。
【讨论】:
但是为什么我仍然收到Whitelabel Error Page
错误。
@SazzadHissainKhan 请参考:***.com/questions/25356781/…
@SazzadHissainKhan 如果它帮助你获得了你想要的东西,你可以投票并将其标记为已验证吗?【参考方案2】:
/错误处理程序已在 Spring Boot 应用程序中定义。您可以禁用它:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
@SpringBootApplication(exclude = ErrorMvcAutoConfiguration.class)
public class DemoApplication
public static void main(String[] args)
SpringApplication.run(DemoApplication.class, args);
或将您的 /error 路径更改为其他路径
【讨论】:
@SazzadHissainKhan,我创建了相同的应用程序 - 它工作正常。显示您的日志或异常堆栈跟踪 @SazzadHissainKhan 显示您的项目结构。可能你也有同样的问题:***.com/questions/31134333/… 在帖子中添加了结构 它看起来像你改变了你的路径,但不要添加 ErrorMvcAutoConfiguration.class【参考方案3】:错误背后的原因是在您的 ResourceController 中,您有绑定 URL“/error”,Spring Boot BasicErrorController 在内部已经使用它来显示白标签错误页面。
您可以将 requestMapping 从 URL "/error" 更改为其他内容以消除错误。如果您对使用 URL 或自定义 Spring REST 错误处理的默认行为感兴趣,可以在此处阅读更多内容。
https://mkyong.com/spring-boot/spring-rest-error-handling-example/
【讨论】:
以上是关于为啥我的 Spring Boot 应用程序出现异常?的主要内容,如果未能解决你的问题,请参考以下文章
为啥在尝试将 Hibernate Spatial 用于 Spring Boot 项目时出现此错误?实例化异常