java - Spring Boot 中多个项目中的错误处理程序

标签 java spring-boot exception jar microservices

我目前正在 Spring Boot 中的不同项目中使用不同的服务。

我已经制作了一个错误处理程序,它将成为所有项目的 JAR,所以当我有

com.my.package 在所有项目中,错误处理程序工作正常。

com.my.package.Controller
com.my.package.Entity
...

我的异常响应:

    "code": 400,
    "message": "'id' must be java.lang.Integer",
    "status": "BAD_REQUEST"
}

但问题是当我有

com.my.package.p1 -> 项目 p1

com.my.package.p2 -> 对于项目 p2

com.my.package.p1.Controller
com.my.package.p1.Entity
...
com.my.package.p2.Controller
com.my.package.p2.Entity
...

错误处理程序似乎在其工作中失败,默认处理程序解决了错误,而不是我的。

{
    "timestamp": "2019-09-30T23:58:57.379+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: \"j\"",
    "path": "/api/department/j"
}

这些是我的 JAR 中的类(class)

//@RestControllerAdvice(basePackages = "com.my.package")
@ControllerAdvice
public class GlobalControllerExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

      //405
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    protected GlobalException processMethodNotSupportedException(final HttpRequestMethodNotSupportedException ex) {
        LOGGER.error("Method not allowed");  
        return new GlobalException(405, "Method not allowed",HttpStatus.METHOD_NOT_ALLOWED);
    }

...

如您所见,我尝试使用@RestControllerAdvice和@ControllerAdvice,并尝试使用注释basePackages,但没有成功的结果。

我还做了另一门课:

@Configuration
@ComponentScan("com.my.package")
@EntityScan("com.my.package")
public class SharedBeanReference {

}

但结果相同。

有人可以帮我弄清楚发生了什么吗?

最佳答案

您可以尝试使用以下注释指定 Controller 建议的基本包:

@ControllerAdvice(basePackages = "com.my.package")
public class GlobalControllerExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

      //405
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    protected GlobalException processMethodNotSupportedException(final HttpRequestMethodNotSupportedException ex) {
        LOGGER.error("Method not allowed");  
        return new GlobalException(405, "Method not allowed",HttpStatus.METHOD_NOT_ALLOWED);
    }

...

For @ExceptionHandler methods, a root exception match will be preferred to just matching a cause of the current exception, among the handler methods of a particular advice bean. However, a cause match on a higher-priority advice will still be preferred over any match (whether root or cause level) on a lower-priority advice bean. As a consequence, please declare your primary root exception mappings on a prioritized advice bean with a corresponding order.

或者您可以尝试以下操作:

  • 尝试使用 @Priority@Order 注释为 Controller 建议类提供更高的优先级或顺序,以便其匹配。
  • 您需要确保 @ControllerAdvice 类位于您的组件扫描基础包下。

引用doc 官方Doc

关于java - Spring Boot 中多个项目中的错误处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58176633/

相关文章:

python - Flask 没有注册异常

java - 在 Java 运行时从 zip 文件读取 ESRI shapefile - DataStoreFinder.getDataStore(connectParameters) 返回 null

java - 计算多边形内的点

java - Spring Boot - Hibernate,异常时回滚成功保存

c# - 修改现有程序以更好地处理异常

exception - 在 Kotlin stdlib 中对常见的 java.lang.*Exceptions 进行类型别名的目的是什么?

java - 正则表达式分配不正确

java - 使用ViewPager后TabItem图标消失

Spring Security OAuth2 accessToken

java - 如何使用分页进行动态 Spring (Boot) JPA 查询?