我有一个休息端点,该端点使用带有hibernate-validator的javax验证。
@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;
如果我取消“有效”注释,则会执行此端点。 但是,一旦添加它,我将得到以下信息:
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"}
我还有其他正在使用验证的端点,但是该端点无效。我一直在网上寻找某些原因,导致这种情况发生,而我却空无一人。
答案 0 :(得分:2)
最近有一个类似的问题报告给我们:这是JDK在不知道注释的情况下抛出NPE的错误。
我想知道您是否正在使用运行时不存在的注释(例如,通过使用Bean Validation 2.0注释,而在运行时具有Bean Validation 1.1)。