我正在使用Spring 4.1.1与Hibernate Validator 5.1.3。我正在尝试使用JSR303验证验证方法参数,并且它无法检索验证方法的参数名称,因为spring在接口上应用验证,但参数的名称在编译的实现类中。
界面: 代码:
@Validated
public interface NomenclatureVenteRest {
public Response recupererArborescenceNomenclatureVente(
@FieldValidation(required="dateAppli") StimeDate dateAppli,
@FieldValidation(check = GENE_M010_PAYS) String codePays,
@FieldValidation(check = GENE_M010_ENSEIGNE) List<String> listeCodeEns) throws StimeFunctionalException;
}
实施: 代码:
@Path("/")
@Service(value = "nomenclatureVenteRestImpl")
public class NomenclatureVenteRestImpl implements NomenclatureVenteRest {
@GET
@Path("/")
@Produces(RestUtils.UTF8_ENCODED_JSON)
@Override
public Response recupererArborescenceNomenclatureVente(
@QueryParam("dateAppli") StimeDate dateAppli,
@QueryParam("codePays")String codePays,
@QueryParam("listeCodeEns")List<String> listeCodeEns) throws StimeFunctionalException{
...Business logic...
}
}
@FieldValidation是我的自定义约束验证
@Target({ PARAMETER, ANNOTATION_TYPE ,FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = FieldValidator.class)
@Documented
public @interface FieldValidation {
String message() default "";
Class<?>[] groups() default {};
boolean required() default false;
FieldTypeValidator[] check() default {};
Class<? extends Payload>[] payload() default {};
}
我想在我的验证器中使用参数名称:
public class FieldValidator implements ConstraintValidator<FieldValidation, Object>, ApplicationContextAware {
ApplicationContext context;
private boolean fieldRequired;
private FieldTypeValidator[] fieldCheck;
@Override
public final void initialize(final FieldValidation annotation) {
fieldCheck = annotation.check();
fieldRequired = annotation.required();
}
@SuppressWarnings("unchecked")
@Override
public boolean isValid(final Object value, ConstraintValidatorContext constraintValidatorContext) {
//If I take look a the constraintValidatorContext.methodParameterNames contains ["arg0", "arg1", "arg2"] , but I need the real parameter names ["dateAppli","codePays","listeCodeEns"]
return true;
}
Jsr303配置:
<!-- JSR 303 validation -->
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean
class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
<property name="validator" ref="validator" />
</bean>
<bean
class="org.springframework.validation.beanvalidation.BeanValidationPostProcessor">
<property name="validator" ref="validator" />
</bean>
问候,
RégisLIMARE