buguge - Keep it simple,stupid

知识就是力量,但更重要的,是运用知识的能力why buguge?

导航

hibernate.validator.constraints.NotEmpty校验请求参数报错java.lang.NoClassDefFoundError: javax/el/PropertyNotFoundException

spring maven项目,使用hibernate validator 注解形式校验方法的入参对象。

hibernate-validator版本:5.0.2.Final

validation-api 版本:1.1.0.Final

 

model类中有个名为order_time的属性用@NotEmpty注解:

package com.emax.paycenter.dto.external;

import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.URL;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * 扫码支付 Request Model
 * Created by 云龙 on 2017/6/1.
 */
public class QRCodeRequest extends BaseRequest {

    private static final long serialVersionUID = -3640955104188613350L;

    /**
     * 订单号
     */
    @NotNull(message = "order_no不能为空!")
    private String order_no;
    /**
     * 支付金额,以分为单位
     */
    @Min(value = 1,message = "pay_money最小值1!")
    public int pay_money;
    /**
     * 接收支付中心通知的URL,需给绝对路径,255字符内格式如:http://wap.tenpay.com/tenpay.asp,确保支付中心能通过http访问该地址
     */
    @URL(protocol="http",message="notify_url无效的URL地址!")
    @NotNull(message = "notify_url不能为空!")
    private String notify_url;
    /**
     * 订单时间(格式:yyyyMMddHHmmss)
     */
    @NotEmpty(message = "order_time不能为空!")
    private String order_time;
    /**
     * 商品名称
     */
    @NotNull(message = "goods_name不能为空!")
    private String goods_name;
    /**
     * 备注
     */
    private String remark;
    /**
     * 几分钟有效
     */
    private String valid_minutes;
    /**
     * 支付渠道 酷宝支付, 青岛中信
     */
    @NotNull(message = "third_pay_platform不能为空!")
    private String third_pay_platform;
    /**
     * 支付通道类型(12:微信扫码,22:支付宝扫码)
     */
    @NotNull(message = "pay_channel不能为空!")
    private int pay_channel;
    
    //
    getter/setter (略)
    
}

 

BeanValidators工具类:

package com.emax.paycenter.utils;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import java.util.Set;

/**
 * JSR303 Validator(Hibernate Validator)工具类.
 * 
 * ConstraintViolation中包含propertyPath, message 和invalidValue等信息.
 * 提供了各种convert方法,适合不同的i18n需求:
 * 1. List<String>, String内容为message
 * 2. List<String>, String内容为propertyPath + separator + message
 * 3. Map<propertyPath, message>
 * 
 * 详情见wiki: https://github.com/springside/springside4/wiki/HibernateValidator
 * @author calvin
 * @version 2013-01-15
 */
public class BeanValidators {

    /**
     * 调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException.
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void validateWithException(Validator validator, Object object, Class<?>... groups)
            throws ConstraintViolationException {
        Set<ConstraintViolation<Object>>  constraintViolations = validator.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
            StringBuffer sb=new StringBuffer();
            for (ConstraintViolation constraintViolation : constraintViolations) {
                sb.append(constraintViolation.getMessage());
            }
            throw new RuntimeException(sb.toString());
        }

    }
}

 

编写如下testcase方法:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
public class ValidatorTest {
    @Autowired
    private Validator validator;
    
    @Test
    public void notEmptyAttributeTest() throws Exception {
        QRCodeRequest pojo=new QRCodeRequest();
        pojo.setOrder_time("");
        
        try {
            BeanValidators.validateWithException(validator, pojo);
        } catch (RuntimeException e) {
            System.out.println("validerror:" + e.getMessage());
        }
    }
}

执行testcase报错:

java.lang.NoClassDefFoundError: javax/el/PropertyNotFoundException

    at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateExpression(ResourceBundleMessageInterpolator.java:227)
    at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateMessage(ResourceBundleMessageInterpolator.java:187)
    at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolate(ResourceBundleMessageInterpolator.java:120)
    at org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator.interpolate(LocaleContextMessageInterpolator.java:49)
    at org.hibernate.validator.internal.engine.ValidationContext.interpolate(ValidationContext.java:370)
    at org.hibernate.validator.internal.engine.ValidationContext.createConstraintViolation(ValidationContext.java:284)
    at org.hibernate.validator.internal.engine.ValidationContext.createConstraintViolations(ValidationContext.java:246)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:289)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:133)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateComposingConstraints(ConstraintTree.java:233)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:102)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:91)
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:85)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:478)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:424)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:388)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:340)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:158)
    at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:199)
    at com.emax.paycenter.utils.BeanValidators.validateWithException(BeanValidators.java:39)
    at com.emax.paycenter.backend.dao.mapper.ValidatorTest.notEmptyAttributeTest(ValidatorTest.java:50)
    at ...
Caused by: java.lang.ClassNotFoundException: javax.el.PropertyNotFoundException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 48 more
java.lang.NoClassDefFoundError: javax/el/PropertyNotFoundException

有时报下面异常=========================================

java.lang.NoClassDefFoundError: javax/el/ELContext 
    at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateExpression(ResourceBundleMessageInterpolator.java:227)
    at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateMessage(ResourceBundleMessageInterpolator.java:187)
    at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolate(ResourceBundleMessageInterpolator.java:120)
    at org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator.interpolate(LocaleContextMessageInterpolator.java:49)
    at org.hibernate.validator.internal.engine.ValidationContext.interpolate(ValidationContext.java:370)
    at org.hibernate.validator.internal.engine.ValidationContext.createConstraintViolation(ValidationContext.java:284)
    at org.hibernate.validator.internal.engine.ValidationContext.createConstraintViolations(ValidationContext.java:246)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:289)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:133)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateComposingConstraints(ConstraintTree.java:233)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:102)
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:91)
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:85)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:478)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:424)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:388)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:340)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:158)
    at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:199)
    at com.emax.paycenter.utils.BeanValidators.validateWithException(BeanValidators.java:39)
    at com.emax.paycenter.backend.dao.mapper.ValidatorTest.notEmptyAttributeTest(ValidatorTest.java:50)
    at ...
Caused by: java.lang.ClassNotFoundException: javax.el.ELContext
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 48 more
java.lang.NoClassDefFoundError: javax/el/ELContext

 

解决办法:

  • 找同事协助,尝试引入jstl,不奏效。
  • 属性不用NotEmpty注解,改用NotBlank注解。(其实,我们所需的验证项正是NotBlank,而非NotEmpty)
  • 参考http://blog.csdn.net/bsg_24/article/details/49494975,将hibernate validator的版本降到4.3.1.Final。

 

posted on 2017-08-17 19:37  buguge  阅读(3123)  评论(0编辑  收藏  举报