在ControllerAdvice

时间:2018-06-23 13:17:55

标签: java spring spring-mvc spring-boot

这是一个问题:我有一个采用输入模型的控制器。可以说

public class AppUserUpdateData {

  @NotNull
  @Size(min = 1, max = 50)
  protected String login;  
  @JsonDeserialize(using = MyDateTimeDeserializer.class)  
  protected Date startWorkDate;
  *************
  other properties and methods
  *************
}

问题是,当我想限制某个日期的结束时,尽管我在代码中处理了这种情况,但最终还是得到了HTTP异常400而没有任何消息! 这是一个控制器:

 @RequestMapping(
      value = "/users/{userId}", method = RequestMethod.PUT,
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public @ResponseBody AbstractSuccessResult updateUser(@PathVariable Long userId,
      @RequestBody AppUserUpdateData  appUserUpdateRequest, HttpServletRequest request) {    
    AbstractSuccessResult response = new AbstractSuccessResult();
    appUserService.updateUser(appUserUpdateRequest, userId);
    return response;
  }

这里是反序列化器:

public class MyDateTimeDeserializer extends JsonDeserializer<Date> {

  @Override
  public Date deserialize(JsonParser jsonParser, DeserializationContext context)
      throws IOException, JsonProcessingException {
    try {
      return DataTypeHelper.stringToDateTime(jsonParser.getText());
    } catch (MyOwnWrittenException ex) {
      throw ex;
    }
  }  
}

DataTypeHelper.stringToDateTime中有一些验证阻止了无效的日期字符串。 还有一个我的例外处理程序:

@ControllerAdvice
public class MyExceptionHandler extends ResponseEntityExceptionHandler {

  @ExceptionHandler({ MyOwnWrittenException .class})
  protected ResponseEntity<Object> handleInvalidRequest(RuntimeException exc, 
    WebRequest request) {

    MyOwnWrittenException ex = (MyOwnWrittenException) exc;
    BasicErrorMessage message; = new BasicErrorMessage(ex.getMessage());    
    AbstractUnsuccessfulResult result = new AbstractUnsuccessfulResult(message);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    return handleExceptionInternal(exc, result, headers, HttpStatus.BAD_REQUEST, request);
  }
}

问题在于,当引发MyDateTimeDeserializer中的异常时,它不会落入MyExceptionHandler中,但是我不明白为什么吗?我究竟做错了什么? 响应中只是一个空的响应,代码为400(

UPD 感谢@Joe Doe的回答,问题已解决。这是我更新的处理程序:

@Order(Ordered.HIGHEST_PRECEDENCE)    
@ControllerAdvice
public class MyExceptionHandler extends ResponseEntityExceptionHandler {

  @ExceptionHandler({ MyOwnWrittenException .class})
  protected ResponseEntity<Object> handleInvalidRequest(RuntimeException exc, 
    WebRequest request) {

    MyOwnWrittenException ex = (MyOwnWrittenException) exc;
    BasicErrorMessage message; = new BasicErrorMessage(ex.getMessage());    
    AbstractUnsuccessfulResult result = new AbstractUnsuccessfulResult(message);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    return handleExceptionInternal(exc, result, headers, HttpStatus.BAD_REQUEST, request);
  }

  @Override
  protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
      HttpHeaders headers, HttpStatus status, WebRequest request) {
    Throwable cause = ex.getCause();
    String message = null;
    if (cause instanceof JsonMappingException) {
      if (cause.getCause() instanceof MyOwnWrittenException) {
        return handleInvalidRequest((RuntimeException) cause.getCause(), request);
      } else {
        message = cause.getMessage();
      }
    } else {
      message = ex.getMessage();
    }
    AbstractUnsuccessfulResult result = new AbstractUnsuccessfulResult(
        new BasicErrorMessage(message));
    headers.setContentType(MediaType.APPLICATION_JSON);
    return handleExceptionInternal(ex, result, headers, HttpStatus.BAD_REQUEST, request);
  }
}

UPD 在我的项目中,如果没有注释@Order(Ordered.HIGHEST_PRECEDENCE),它将无法正常工作 我认为这是由于项目中ControllerAdvices的数量

1 个答案:

答案 0 :(得分:1)

在调用控制器中的updateUser之前,必须先解析其参数。这是HandlerMethodArgumentResolverComposite进入的地方,并委托给一个预先注册的HandlerMethodArgumentResolver-在这种情况下,它委托给RequestResponseBodyMethodProcessor

通过委派,我的意思是调用解析器的resolveArgument方法。该方法从解串器中间接调用deserialize方法,该方法抛出类型MyOwnWrittenException的异常。问题在于此异常被包装在另一个异常中。实际上,当它传播回resolveArgument时,它的类型为HttpMessageNotReadableException

因此,您需要捕获MyOwnWrittenException类型的异常,而不是在自定义异常处理程序中捕获HttpMessageNotReadableException。然后,在处理这种情况的方法中,您可以检查“原始”异常是否确实为MyOwnWrittenException-您可以通过重复调用getCause方法来做到这一点。就我而言(可能与您的情况相同),我需要调用两次getCause以“解开”原始异常(HttpMessageNotReadableException-> JsonMappingException-> {{1 }}。

请注意,您不能在异常处理程序中简单地将MyOwnWrittenException替换为MyOwnWrittenException,因为它(在运行时)与另一种专门用于处理后一种类型的异常的方法(称为{{ 1}}。

总而言之,您可以执行以下操作:

HttpMessageNotReadableException