自定义Bean验证UnexpectedType异常Spring 3.2

时间:2017-07-25 14:17:28

标签: java spring hibernate spring-mvc bean-validation

我是一个使用Spring 3.2的应用程序的新手。 Project是关于升级的,我们将Java 1.6升级到1.7。升级后,我面临着字段组上的自定义bean验证问题。

我有谷歌并了解验证,看起来不错。请注意,这非常适用于Java 1.6,我也进行了代码比较。

Controller.java

@RequestMapping(value = Constants.URL, method = RequestMethod.POST)
    public ModelAndView process(
            @ModelAttribute("Bean") Bean Bean,
            BindingResult result, SessionStatus status,
            @RequestParam(Constants.WIZARD_PAGE) int currentPage,
            ModelMap modelMap, Model model, Locale locale,
            RedirectAttributes redirectAttributes, HttpServletRequest request,
            HttpServletResponse response) {
isNotValid(Bean, result, Interface.class);
}

public boolean isNotValid(Object target, Errors errors, Class<?>... groups) {
        Set<ConstraintViolation<Object>> result = validator.validate(target,
                groups);

Bean Class

    @NotEmpty
    @PhoneComposite(phone1 = "phone1", phone2 = "phone2", phone3 = "phone3", groups = {
            Interface.class, Interface2.class })
    private Phone phone = new Phone();

    Phone Bean Class

    public Phone() {
        super();
    }

    public Phone(String phone1, String phone2, String phone3) {
        this();
        this.phone1 = phone1;
        this.phone2 = phone2;
        this.phone3 = phone3;
    }

    //having getter and setter too

约束类

@Constraint(validatedBy = { PhoneCompositeValidator.class })
@Documented
@Target({ ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface PhoneComposite {

    /**
     * Message.
     * 
     * @return the string
     */
    String message() default "{invalid.phone}";

    /**
     * Groups.
     * 
     * @return the class[]
     */
    Class<?>[] groups() default {};

    /**
     * Payload.
     * 
     * @return the class<? extends payload>[]
     */
    Class<? extends Payload>[] payload() default {};

    /**
     * phone1.
     * 
     * @return the string
     */
    String phone1();

    /**
     * phone2.
     * 
     * @return the string
     */
    String phone2();

    /**
     * phone3.
     * 
     * @return the string
     */
    String phone3();

}

Validator Class

public class PhoneCompositeValidator  implements ConstraintValidator<PhoneComposite, Object>{

    /** The phone1 name. */
    private String phone1Name; 

    /** The phone2 name. */
    private String phone2Name; 

    /** The phone3 name. */
    private String phone3Name;

    /**
     * Initialize.
     *
     * @param arg0 the arg0
     */
    @Override
    public void initialize(PhoneComposite arg0) {
        phone1Name = arg0.phone1() ;
        phone2Name = arg0.phone2() ;
        phone3Name = arg0.phone3() ;

    }

    /* (non-Javadoc)
     * @see javax.validation.ConstraintValidator#isValid(java.lang.Object, javax.validation.ConstraintValidatorContext)
     */
    /**
     * Checks if is valid.
     *
     * @param arg0 the arg0
     * @param arg1 the arg1
     * @return true, if is valid
     */
    @Override
    public boolean isValid(Object arg0, ConstraintValidatorContext arg1) {
        String phone1 = null ;
        String phone2 = null ;
        String phone3 = null ;
        try {
            phone1 = BeanUtils.getProperty(arg0, phone1Name) ;
            if (StringUtils.isEmpty(phone1) || (!phone1.matches("\\d{3}"))) {
                LoggerService.error("Phone1 Not Valid");
                return false ;
            }
            phone2 = BeanUtils.getProperty(arg0, phone2Name) ;
            if (StringUtils.isEmpty(phone2) || (!phone2.matches("\\d{3}"))) {
                LoggerService.error("Phone2 Not Valid");
                return false ;
            }
            phone3 = BeanUtils.getProperty(arg0, phone3Name) ;
            if (StringUtils.isEmpty(phone3) || (!phone3.matches("\\d{4}"))) {
                LoggerService.error("Phone3 Not Valid");
                return false ;
            }
            return true ;
        }   catch (Exception e) {
            e.printStackTrace() ;
            return false;
        }

    } 

}

库的使用

                      

    <dependency org="javax.validation" name="validation-api" rev="1.0.0.GA" transitive="false">
         <artifact name="validation-api" ext="jar"/>
    </dependency>

    <dependency org="org.slf4j" name="slf4j-api" rev="1.7.5" transitive="false">
         <artifact name="slf4j-api" ext="jar"/>
    </dependency>
    <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.7.5" transitive="false">
         <artifact name="slf4j-log4j12" ext="jar"/>
    </dependency>

注意:这在Java 1.6中有效,也没有为升级而进行的代码更改。

例外:

Caused by: javax.validation.UnexpectedTypeException: No validator could be found for type bean.Phone. See: @Size at private bean.Phone bean.Bean.phone
at org.apache.bval.jsr303.AnnotationProcessor.checkOneType(AnnotationProcessor.java:328)
at org.apache.bval.jsr303.AnnotationProcessor.getConstraintValidator(AnnotationProcessor.java:303)
at org.apache.bval.jsr303.AnnotationProcessor.applyConstraint(AnnotationProcessor.java:243)
at org.apache.bval.jsr303.AnnotationProcessor.processAnnotation(AnnotationProcessor.java:151)
at org.apache.bval.jsr303.AnnotationProcessor.processAnnotations(AnnotationProcessor.java:92)
at org.apache.bval.jsr303.AnnotationProcessor.applyConstraint(AnnotationProcessor.java:279)
at org.apache.bval.jsr303.AnnotationProcessor.processAnnotation(AnnotationProcessor.java:151)
at org.apache.bval.jsr303.AnnotationProcessor.processAnnotations(AnnotationProcessor.java:92)
at org.apache.bval.jsr303.Jsr303MetaBeanFactory.processClass(Jsr303MetaBeanFactory.java:134)
at org.apache.bval.jsr303.Jsr303MetaBeanFactory.buildMetaBean(Jsr303MetaBeanFactory.java:95)
at org.apache.bval.MetaBeanBuilder.buildForClass(MetaBeanBuilder.java:129)
at org.apache.bval.MetaBeanManager.findForClass(MetaBeanManager.java:102)
at org.apache.bval.jsr303.ClassValidator.validate(ClassValidator.java:140)
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:199)

请帮助我找出它可能引用哪个jar依赖,因为我怀疑有一些java版本。

0 个答案:

没有答案