为[class java.lang.Exception]映射的不明确@ExceptionHandler方法:{public final org.springframework.http.ResponseEntity

问题描述 投票:0回答:1

我正在将 Spring Boot 微服务从版本 2021.0.9 迁移到 2023.0.1。我有一个带有共享库的微服务,用于处理异常。

Spring Boot 微服务:

import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
@Order(Integer.MAX_VALUE)
public class CommonControllerAdvice extends ResponseEntityExceptionHandler {

  private static final String LABEL = "Label";

  @ExceptionHandler(Exception.class)
  public final ResponseEntity<Object> handleHystrixBadRequestException(
    .........
    return null;
  }
}

共享库:

import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
@Order(Integer.MAX_VALUE)
public class CommonControllerAdvice extends ResponseEntityExceptionHandler {
  
  @ExceptionHandler(Exception.class)
  public final ResponseEntity<Object> handleException(Exception ex) {
    ......
    return ....;
  }
}

启动时出错:

Error creating bean with name 'handlerExceptionResolver' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Failed to instantiate [org.springframework.web.servlet.HandlerExceptionResolver]: Factory method 'handlerExceptionResolver' threw exception with message: Ambiguous @ExceptionHandler method mapped for [class java.lang.Exception]: {public final org.springframework.http.ResponseEntity com.atlas.mpp.transaction.search.web.rest.SearchControllerAdvice.handleHystrixBadRequestException(java.lang.Exception), public final org.springframework.http.ResponseEntity com.error.web.handlers.CommonControllerAdvice.handleException(java.lang.Exception)}
.........
Failed to instantiate [org.springframework.web.servlet.HandlerExceptionResolver]: Factory method 'handlerExceptionResolver' threw exception with message: Ambiguous @ExceptionHandler method mapped for [class java.lang.Exception]: {public final org.springframework.http.ResponseEntity com.microservice.SearchControllerAdvice.handleHystrixBadRequestException(java.lang.Exception), public final org.springframework.http.ResponseEntity com.lib.web.handlers.CommonControllerAdvice.handleException(java.lang.Exception)}
.......
Ambiguous @ExceptionHandler method mapped for [class java.lang.Exception]: {public final org.springframework.http.ResponseEntity com.microservice.web.rest.SearchControllerAdvice.handleHystrixBadRequestException(java.lang.Exception), public final org.springframework.http.ResponseEntity com.lib.web.handlers.CommonControllerAdvice.handleException(java.lang.Exception)}

此代码在版本 2021.0.9 中工作正常,但在版本 2023.0.1 中出现上述错误。新版本似乎有重组。有哪些可能的方法可以解决这个问题?

spring spring-boot spring-mvc
1个回答
0
投票

错误在于您的 Spring 应用程序检测到多个提供

@ExceptionHandler(Exception.class)
的类,并且 Spring 告诉您只需要存在一个类,否则会使 Spring 感到困惑。

该错误指定以下方法/类提供了对

@ExceptionHandler(Exception.class)
的处理:

1.

com.microservice.web.rest.SearchControllerAdvice.handleHystrixBadRequestException(java.lang.Exception)

  1. com.lib.web.handlers.CommonControllerAdvice.handleException(java.lang.Exception)

要解决此问题,请选择一个处理程序(最好是公共库)并删除另一个

@ExceptionHandler(Exception.class)
,或者更改现有处理程序之一以处理
Exception
之外的更具体的异常子类,以避免冲突。

© www.soinside.com 2019 - 2024. All rights reserved.