首页 > 解决方案 > 如何正确抛出和检测此异常

问题描述

我正在使用 @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

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

有什么建议吗?

标签: springexceptioncontroller-advice

解决方案


异常处理程序处理处理程序方法中发生的异常(请参阅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/


推荐阅读