簡體   English   中英

為什么我的 Spring Boot 應用程序出現異常?

[英]Why am I getting exception in my Spring Boot Application?

我的 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);
    }
}

獲得白標錯誤頁面后,我在我的一個控制器中映射了/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

/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 路徑更改為其他內容

錯誤背后的原因是在您的ResourceController 中,您綁定了 URL“/error”,Spring Boot BasicErrorController 已在內部使用該 URL 來顯示白標錯誤頁面。

您可以將 requestMapping 從 URL "/error" 更改為其他內容以消除錯誤。 如果您對使用 URL 或自定義 Spring REST 錯誤處理的默認行為感興趣,可以在此處閱讀更多內容。

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

Spring 告訴您存在一個不明確的映射。 由於com.example.demo.controller.ResourceControllerorg.springframework.boot.autoconfigure.web.servlet.error.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 的路徑都以: <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。

在我看來,我會建議您采用第一種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM