Spring 如何正确引发和检测此异常
我使用@ControllerAdvice来检测应用程序中抛出的异常 尝试在创建类期间引发异常:Spring 如何正确引发和检测此异常,spring,exception,controller-advice,Spring,Exception,Controller Advice,我使用@ControllerAdvice来检测应用程序中抛出的异常 尝试在创建类期间引发异常: public void setStatus(String status) throws InvalidInputStatusException{ if(checkIfStatusIsAllowed(status)) { this.status = status; } else { throw new InvalidInpu
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);
}
}
我假设它无法检测到预期的异常,因为在它之前抛出了另一个异常,但这不是我想要的
有什么建议吗?异常不发生在业务代码中,而是发生在将请求解析为请求表示对象的过程中。SpringWeb将解析请求期间发生的任何类型的异常视为表示级别的错误,而不是业务级别的错误,因此,不会调用异常处理程序
因为您试图在这里强制执行业务规则,所以我建议将其移出表示对象的setter方法,并为其找到更好的位置。例如,将此逻辑放在业务实体或您的一个服务中,或者至少放在接受请求的控制器方法中。首先,您的RekvisiJonRestControllerExceptionHandler应该扩展自ResponseEntityExceptionHandler。 如果您返回ResponseEntity,它将包装您的值类(RekvisisjonRESTErrorResponse)。
在这里,当json序列化时,在通知之后生成异常。异常处理程序处理处理处理程序方法中发生的异常(请参阅)。您看到的异常发生在前面,而Spring正试图将JSON请求主体转换为一个
更新的eKVisisJonStatusRequest
。Jackson JSON反序列化程序正在调用setStatus
方法并遇到异常,Spring认为这意味着HTTP正文不可读(因为Jackson无法反序列化)
看看Spring MVC如何处理验证:
"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