Spring 引导应用程序的 Bean 验证失败

Bean validation fails for Spring Boot application

我有一个使用 javax 验证和 hibernate-validator 的休息端点。

@PostMapping(path = "/send")
public ResponseEntity<?> send(@Valid @RequestBody DisbursementProcessingDto processingDto,
                                  @RequestHeader(HttpHeaders.USER_AGENT) String userAgent, 
                                  @RequestHeader(name = X_REQUEST_ID, required = false) Optional<String> xRequestId,
                                  BindingResult bindingResult) {

bean 具有以下验证

@NotNull(message = "A valid correlation Id must be provided.")
private UUID correlationId;
@NotNull(message = "A non null and postive amount is required")
@Positive(message = "A non null and postive amount is required")
private BigDecimal totalAmount;
@NotNull(message = "A non null and postive count is required")
@Positive(message = "A non null and postive count is required")
private Long totalCount;

如果我取消 Valid 注释,这个端点就会执行。 但是一旦我添加它,我就会得到以下信息:

Caused by: java.lang.NullPointerException: null
    at sun.reflect.annotation.TypeAnnotationParser.mapTypeAnnotations(TypeAnnotationParser.java:356)
    at sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl.<init>(AnnotatedTypeFactory.java:139)
    at sun.reflect.annotation.AnnotatedTypeFactory.buildAnnotatedType(AnnotatedTypeFactory.java:65)
    at sun.reflect.annotation.TypeAnnotationParser.buildAnnotatedType(TypeAnnotationParser.java:79)
    at java.lang.reflect.Field.getAnnotatedType(Field.java:1170)
    at org.hibernate.validator.internal.metadata.provider.TypeAnnotationAwareMetaDataProvider.findTypeAnnotationConstraintsForMember(TypeAnnotationAwareMetaDataProvider.java:65)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findPropertyMetaData(AnnotationMetaDataProvider.java:244)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getFieldMetaData(AnnotationMetaDataProvider.java:227)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.retrieveBeanConfiguration(AnnotationMetaDataProvider.java:137)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfiguration(AnnotationMetaDataProvider.java:125)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfigurationForHierarchy(AnnotationMetaDataProvider.java:108)
    at org.hibernate.validator.internal.metadata.BeanMetaDataManager.createBeanMetaData(BeanMetaDataManager.java:203)
    at org.hibernate.validator.internal.metadata.BeanMetaDataManager.getOrCreateBeanMetaData(BeanMetaDataManager.java:231)
    at org.hibernate.validator.internal.metadata.BeanMetaDataManager.isConstrained(BeanMetaDataManager.java:174)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:195)
    at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:119)
    at org.springframework.boot.autoconfigure.web.WebMvcValidator.validate(WebMvcValidator.java:69)
    at org.springframework.validation.DataBinder.validate(DataBinder.java:892)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.validateIfApplicable(AbstractMessageConverterMethodArgumentResolver.java:270)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:133)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    ... 61 common frames omitted\n"}

我有其他使用有效验证的端点,但此端点无效。我一直在网上寻找发生这种情况的某种原因,但我一无所获。

我们最近向我们报告了一个类似的问题:这是 JDK 在不知道注释时抛出 NPE 的错误。

我想知道您是否有机会使用运行时不存在的注释(例如使用 Bean Validation 2.0 注释,而您在运行时使用 Bean Validation 1.1)。