我正在使用spring-boot-starter-web:2.1.6.RELEASE
编写代码,该代码暂时依赖于hibernate-validator:6.0.17
。
然后出现以下错误。
java.lang.IllegalArgumentException: HV000116:
type is not a reference type: ? extends java.math.BigDecimal
来自
default @NotNull BigDecimal divide(
@Size(min = 2, max = 2) @NotNull
List<@NotNull ? extends BigDecimal> positioned) {
...
}
尤其是从<@NotNull ? extends BigDecimal>
起。
我做错了什么?
这是堆栈跟踪
java.lang.IllegalArgumentException: HV000116: type is not a reference type: ? extends java.math.BigDecimal
at org.hibernate.validator.internal.util.Contracts.assertTrue(Contracts.java:73)
at org.hibernate.validator.internal.util.TypeHelper.getErasedReferenceType(TypeHelper.java:193)
at org.hibernate.validator.internal.metadata.core.MetaConstraints.addValueExtractorDescriptorForWrappedValue(MetaConstraints.java:82)
at org.hibernate.validator.internal.metadata.core.MetaConstraints.create(MetaConstraints.java:55)
at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.createTypeArgumentMetaConstraint(AnnotationMetaDataProvider.java:795)
at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.lambda$findTypeUseConstraints$2(AnnotationMetaDataProvider.java:783)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:274)
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findTypeUseConstraints(AnnotationMetaDataProvider.java:784)
at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findTypeArgumentsConstraints(AnnotationMetaDataProvider.java:762)
at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findTypeAnnotationConstraintsForExecutableParameter(AnnotationMetaDataProvider.java:716)
at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getParameterMetaData(AnnotationMetaDataProvider.java:429)
at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findExecutableMetaData(AnnotationMetaDataProvider.java:300)
at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMetaData(AnnotationMetaDataProvider.java:285)
at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMethodMetaData(AnnotationMetaDataProvider.java:272)
at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.retrieveBeanConfiguration(AnnotationMetaDataProvider.java:134)
at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfiguration(AnnotationMetaDataProvider.java:124)
at org.hibernate.validator.internal.metadata.BeanMetaDataManager.getBeanConfigurationForHierarchy(BeanMetaDataManager.java:232)
at org.hibernate.validator.internal.metadata.BeanMetaDataManager.createBeanMetaData(BeanMetaDataManager.java:199)
at org.hibernate.validator.internal.metadata.BeanMetaDataManager.getBeanMetaData(BeanMetaDataManager.java:166)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:265)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:233)
at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:112)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
答案 0 :(得分:1)
您的用法是正确的。我认为这可能是实现错误。
看看休眠解析类型引用TypeHelper#getErasedReferenceType的方式:
public static Class<?> getErasedReferenceType(Type type) {
Contracts.assertTrue( isReferenceType( type ), "type is not a reference type: %s", type );
return (Class<?>) getErasedType( type );
}
isReferenceType
不检查通配符类型TypeHelper#isReferenceType:
private static boolean isReferenceType(Type type) {
return type == null
|| type instanceof Class<?>
|| type instanceof ParameterizedType
|| type instanceof TypeVariable<?>
|| type instanceof GenericArrayType;
}
因此断言在休眠状态下尝试验证列表内容(由<@NotNull ? extends BigDecimal>
导致)失败
但是以某种方式TypeHelper#getErasedType支持通配符类型:
public static Type getErasedType(Type type) {
...
if ( type instanceof WildcardType ) {
Type[] upperBounds = ( (WildcardType) type ).getUpperBounds();
return getErasedType( upperBounds[0] );
}
...
}
我将尝试解决一个问题。