Spring Boot更改覆盖异常响应

huangapple go评论122阅读模式
英文:

Spring boot change override exception responses

问题

Sure, here's the translated content:

我正在尝试自定义异常响应并使用自己的响应结构我正在使用以下方式

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler
{
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResponseEntity<String> handle(Exception ex, HttpServletRequest request)
    {
        // ...
    }
}

但是我无法访问状态码我需要通过 `ResponseStatus` 在异常中定义的状态码

@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
public class ExtendSubscriptionReminderNotExistException extends RuntimeException
{
}
英文:

I am trying to customize exception responses and use my own response structure, I am using below way :

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler
{
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResponseEntity&lt;String&gt; handle(Exception ex, HttpServletRequest request)
    {
...
    }
}

But I have not accessed to the status code, I need status code that defined in exceptions via ResponseStatus:

@ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
public class ExtendSubscriptionReminderNotExistException extends RuntimeException
{
}

答案1

得分: 1

使用 Java 反射机制,您可以这样实现:

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResponseEntity<String> handle(Exception ex, HttpServletRequest request) {
        if (ex instanceof ExtendSubscriptionReminderNotExistException) {
            ResponseStatus status = ExtendSubscriptionReminderNotExistException.class.getAnnotation(ResponseStatus.class);
            return ResponseEntity.status(status.value()).body(ex.getMessage());
        } else {
            // 如果不是 ExtendSubscriptionReminderNotExistException,执行其他操作
        }
    }

以下是有关如何在 Java 中读取注解的有用文章:Java 反射 - 注解

如果您想要覆盖 ResponseStatusExceptionResolver,则应该extends AbstractHandlerExceptionResolver并实现自己的 doResolveException,就像 ResponseStatusExceptionResolver 所做的那样,然后创建一个扩展 WebMvcConfigurationSupport 的配置,并覆盖 configureHandlerExceptionResolvers,然后 Spring 将会选择您自己的异常解析器而不是默认的。其中的逻辑在这里

英文:

With java reflection mechanism, you can do it like so:

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResponseEntity&lt;String&gt; handle(Exception ex, HttpServletRequest request) {
        if (ex instanceOf ExtendSubscriptionReminderNotExistException) {
            ResponseStatus status = ExtendSubscriptionReminderNotExistException.class.getAnnotation(ResponseStatus.class);
            return ResponseEntity.status(status.value()).body(ex.getMessage());
        }else{
            //if it&#39;s not ExtendSubscriptionReminderNotExistException, do sth different
        }
    }

Here is an useful article on how to read annotation in java: Java Reflection - Annotations

If you want to override ResponseStatusExceptionResolver, then you should extends AbstractHandlerExceptionResolver and implement your own doResolveException like ResponseStatusExceptionResolver did, then create a configuration extending WebMvcConfigurationSupport and override configureHandlerExceptionResolvers, then spring will pick up your own exception resolver over the default one. The logic behind this is here.

答案2

得分: 0

我可能在这件事上有所错误,但对我来说,同时使用*@ResponseStatus注解和自定义ErrorHandler*实在没有太多道理。
注解的目的是使你的代码更容易理解,并避免使用这种处理程序。

如果你真的想使用这个处理程序,我建议放弃使用注解,而是在每个异常中存储相应的状态码(例如,作为final static属性)。

英文:

I may be wrong on this one, but to me it doesn't really make sense to use @ResponseStatus annotation and a custom ErrorHandler at the same time.
Annotations are supposed to make your code easier to understand and to avoid using such handlers.

If you really want to use the handler, I'd suggest to drop the annotation and store the corresponding status code in each exception (as a final static attribute for example).

答案3

得分: 0

我们无法更改异常消息。但是,我们可以确定更改代码和类,然后通过覆盖相同的类和相同的代码,但不同的消息来抛出一个新的异常。

英文:

we cannot change exception messages. However determine we can change the code and class, and throw a new one by overriding the same class with the same code and different message.

huangapple
  • 本文由 发表于 2020年10月13日 04:06:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64324670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定