【问题标题】:How to throw and detect this exception correct如何正确抛出和检测此异常
【发布时间】:2020-06-14 18:43:20
【问题描述】:

我正在使用@ControllerAdvice 来检测应用程序中抛出的异常。

在创建类时尝试抛出异常:

    public void setStatus(String status) throws InvalidInputStatusException{
       if(checkIfStatusIsAllowed(status)) {
           this.status = status;
       } else {
           throw new InvalidInputStatusException();
       }

    }

试图捕捉错误:

@ControllerAdvice
public class RekvisisjonRESTControllerExceptionHandler {

    //TODO: Add logger here!

    @ExceptionHandler
    public final ResponseEntity<RekvisisjonRESTErrorResponse> handleException(InvalidInputStatusException e, WebRequest request) {
        //TODO: Do some logging
        return new ResponseEntity<>(new RekvisisjonRESTErrorResponse(HttpStatus.BAD_REQUEST.toString(),
                e.getClass().getName(),
                e.getMessage(), LocalDateTime.now()), HttpStatus.BAD_REQUEST);
    }
}

我想要的是返回上面指定的对象,但我在这里得到了这个废话:

  "error": "Bad Request",
    "message": "JSON parse error: Ugyldig status som input; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Ugyldig status som input\n at [Source: (PushbackInputStream); line: 2, column: 12] (through reference chain: no.pasientreiser.atom.rekvisisjon.controller.dto.UpdateRekvisisjonStatusRequest[\"status\"])",
    "trace": "org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Ugyldig status som input; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Ugyldig status som input\n at [Source: (PushbackInputStream); line: 2, column: 12] (through reference chain: no.pasientreiser.atom.rekvisisjon.controller.dto.UpdateRekvisisjonStatusRequest[\"status\"])\n\tat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:245)\n\tat org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227)\n\tat org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:205)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:158)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:131)\n\tat org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)\n\tat org.springframework.web.me

我假设它无法检测到预期的异常,因为在它之前抛出了另一个异常,但这不是我想要的。

有什么建议吗?

【问题讨论】:

    标签: spring exception controller-advice


    【解决方案1】:

    异常不在您的业务代码中,而是在将请求解析为您的请求表示对象期间发生。 Spring Web 将在解析请求期间发生的任何类型的异常视为表示级别的错误,而不是业务级别的错误,因此不会调用您的异常处理程序。

    由于您尝试在此处强制执行业务规则,我建议将其移出表示对象的 setter 方法并为它找到更好的位置。例如。将此逻辑放在业务实体或您的一项服务中,或者至少放在接受请求的控制器方法中。

    【讨论】:

      【解决方案2】:

      首先,您的 RekvisisjonRESTControllerExceptionHandler 应该从 ResponseEntityExceptionHandler 扩展。 如果您返回 ResponseEntity,它将包装您的值类 (RekvisisjonRESTErrorResponse)。 在这里,您的异常是在建议之后生成的,当 json 序列化时。

      【讨论】:

        【解决方案3】:

        异常处理程序处理您的处理程序方法中发生的异常(请参阅https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc)。您看到的异常发生在前面,而 ​​Spring 正在尝试将 JSON 请求正文转换为 UpdateRekvisisjonStatusRequest。 Jackson JSON 反序列化程序正在调用 setStatus 方法并遇到异常,Spring 认为这意味着 HTTP 正文不可读(因为 Jackson 无法反序列化它)。

        看看 Spring MVC 如何处理验证:https://spring.io/guides/gs/validating-form-input/

        【讨论】:

          猜你喜欢
          • 2012-05-13
          • 1970-01-01
          • 2016-11-11
          • 2021-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-07
          • 1970-01-01
          相关资源
          最近更新 更多