首页 > 解决方案 > 在春季启动 TypeMismatchException.getPropertyName() 总是返回 null

问题描述

在我的 Spring Boot 应用程序中,我有带有 RequestParam Integer clockDrift 的控制器方法。从 Postman 测试它时,如果我提供整数值,一切都按预期工作。但是,如果我在 clockDrift 字段中提供任何字符串值,则请求将发送到处理程序类中的 handleTypeMismatch 方法。我正在尝试从 TypeMismatchException ex 中获取属性名称。我总是变得空虚。如果我检查 ex,通过 Eclipse,我可以在名称下看到“clockDrift”(我看到 F 符号靠近那个。不知道那是什么。如果你能解释 F 的意义,我将不胜感激)。但不确定如何获取名称的值。除了 getPropertyName(),我没有看到任何方法。如何从 TypeMismatchException 对象中获取属性名称?

@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
public class ApiGlobalExceptionHandler extends ResponseEntityExceptionHandler
{
..
..
  @Override
  protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatus status,
      WebRequest request)
  {
    System.out.println(ex.getPropertyName()); -- Returning null
    String message = "Input value '" + ex.getValue() + "'" + " is  invalid. Required type is "
        + ex.getRequiredType().getSimpleName();

    return ResponseEntity.status(status).body(SecurityTransformationUtil.buildServiceMessageResponse(status, message));
  } 

标签: springspring-boottype-mismatchexceptionhandler

解决方案


推荐阅读