当前位置: 首页 > 知识库问答 >
问题:

为什么我在Spring启动应用程序中遇到异常?

汪阳飇
2023-03-14

我的Spring启动应用程序,

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

共有3个答案

和柏
2023-03-14

Spring告诉您存在一个不明确的映射。因为两者都com。实例演示。控制器。ResourceController组织。springframework。靴子自动配置。网状物servlet。错误BasicErrorController具有相同的请求映射/error

因此,您不能在ResourceController类中保留/error

您可以在特定API上添加基本请求映射路径,也可以更改请求映射路径,这会导致不明确的情况。

例子:

第一种方法:在控制器上添加基本映射路径,以消除不明确的情况。

@RestController
@RequestMapping("/resource")
public class ResourceController {

    @RequestMapping("/home")
    String home() {
        return "Hello, Welcome!";
    }

    @RequestMapping("/error")
    String error() {
        return "Error occurred!";
    }
}

注意:在这种情况下,所有API的路径都以:

第二种方法:更改特定API上的请求映射路径,这会导致不明确的情况。

例如:

@RestController
public class ResourceController {

    @RequestMapping("/home")
    String home() {
        return "Hello, Welcome!";
    }

    @RequestMapping("/resource/error")
    String error() {
        return "Error occurred!";
    }
}

注意:在这种情况下,您必须使用

在我看来,我建议你采用第一种方法。

和弘博
2023-03-14

/error处理程序已经在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

堵景天
2023-03-14

错误背后的原因是您的ResourceController中有绑定URL“/error”,Spring Boot BasicErrorController已在内部使用该绑定URL“/error”来显示白标签错误页面。

您可以将requestMapping从URL“/error”更改为其他内容以消除错误。如果您对使用URL或自定义SpringREST错误处理的默认行为感兴趣,您可以在这里阅读更多内容。

https://mkyong.com/spring-boot/spring-rest-error-handling-example/

 类似资料:
  • 我有一个主活动MainActivity,它是我的android应用程序的入口点。它通过发射器正确发射。然而,当我尝试执行startActivity时,我看到活动试图启动的一些调试,一些代码正确启动,但MainActivity从未启动。 我认为这可能与意图有关: 对吗? 从内部活动来看,我有 我甚至看到 10-11 22:23:46.026:INFO/ActivityManager(472):从pi

  • 我最初的程序是为了将数据插入我的数据库。我有4个表,其中插入了数据,为了优化起见,我在一个单独的线程中这样做,因为实体是异步创建的。我正在使用consume方法将实体添加到队列中。 过了一段时间,我决定使用Spring Boot将web api添加到我的应用程序中。Spring Data JPA是需要的,因为一些POST请求将数据插入到我的数据库中。但是Spring Data JPA与我已经在使用

  • 控制器:- 型号:- 我试着让邮递员用以下身体来测试它:-http://localhost:8080/add/score(职位) 我得到了以下错误:

  • 这是:“Parent root=fxmlloader.load(getClass().getResource(”sample.fxml“));” 我不明白剩下的部分,所以我希望你能轻松地解决这个问题:)

  • 我不熟悉测微计和普罗米修斯,我只使用dropwizards的指标。这让我有点困扰,我实际上需要有3个不同的实例来运行以查看指标 带测微计的Spring靴应用程序本身 使用dropwizard,我可以立即公开聚合指标。 这就是为什么我很好奇是否有可能将Prometheus与我的Spring Boots应用程序一起启动以立即公开很好的聚合数据。到目前为止,我还没有找到任何有用的东西,所以我希望这里的某

  • 我是react native的新手,我正在尝试使用android Studio开始我的第一个项目。我遵循react native的“设置开发环境”中的说明,最终使用 然后我在android studio中打开了我的项目来启动AVD,但是gradle抛出了以下错误 错误:评估脚本时出现问题。 无法运行程序“node”(在目录“/home/deadshot/documents/playground/a