简体   繁体   English

Spring 引导中多个项目中的错误处理程序

[英]Error Handler in Multiple Projects in Spring Boot

I'm currently working with different services divided in different projects in Spring Boot.我目前正在使用 Spring Boot 中不同项目中的不同服务。

I have made an error handler which will be a JAR for all the projects, so when I have我已经为所有项目创建了一个错误处理程序,它将是 JAR,所以当我有

com.my.package in all the projects the error handler works fine. com.my.package在所有项目中,错误处理程序都可以正常工作。

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

My Exception Response:我的异常响应:

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

But the problem is when I have但问题是当我有

com.my.package.p1 -> for project p1 com.my.package.p1 -> 用于项目 p1

com.my.package.p2 -> for project p2 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
...

the error handler seems to fail in its job and the default handler resolves the error, rather than mine.错误处理程序似乎在其工作中失败,并且默认处理程序解决了错误,而不是我的。

{
    "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"
}

These are my classes from my JAR这些是我的 JAR 的课程

//@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);
    }

...

As you see, I tried using @RestControllerAdvice and @ControllerAdvice, and trying to use annotation basePackages but there was not succesful result.如您所见,我尝试使用@RestControllerAdvice 和@ControllerAdvice,并尝试使用注释basePackages但没有成功的结果。

I also made another class:我还制作了另一个 class:

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

}

But same result.但同样的结果。

Can somebody help me figuring out what is happening?有人可以帮我弄清楚发生了什么吗?

You could try specifying the basepackage of the controller advice using the annotation like:您可以尝试使用以下注释指定 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.对于 @ExceptionHandler 方法,在特定通知 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.但是,高优先级通知上的原因匹配仍将优先于低优先级通知 bean 上的任何匹配(无论是根级别还是原因级别)。 As a consequence, please declare your primary root exception mappings on a prioritized advice bean with a corresponding order.因此,请在具有相应顺序的优先通知 bean 上声明您的主根异常映射。

Or you could try the following:或者您可以尝试以下方法:

  • Try providing the controller advice class a higher priority or order using the @Priority or @Order annotation so that it gets matched.尝试使用@Priority@Order注释为 controller 建议 class 提供更高的优先级或顺序,以使其匹配。
  • You need make sure that the @ControllerAdvice Class is under your component-scan base package.您需要确保@ControllerAdvice Class 在您的组件扫描基础 package 下。

Reference doc Official Doc参考文档官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM