hibernate.validator.constraints.NotEmpty鏍¢獙璇锋眰鍙傛暟鎶ラ敊java.lang.NoClassDefFoundError- javax-el-Propert

本文介绍了一个SpringMaven项目中使用HibernateValidator进行数据验证的例子,重点展示了如何通过注解如@NotEmpty、@NotNull等来约束DTO对象字段,并提供了一个简单的单元测试案例。
摘要由CSDN通过智能技术生成

spring maven椤圭洰锛屼娇鐢╤ibernate validator 娉ㄨВ褰㈠紡鏍¢獙瀹㈡埛绔殑璇锋眰鍙傛暟銆?/p>

hibernate-validator鐗堟湰锛?.0.2.Final

validation-api 鐗堟湰锛?.1.0.Final

 

model绫讳腑鏈変釜鍚嶄负order_time鐨勫睘鎬х敤@NotEmpty娉ㄨВ锛?/p>

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鏈€灏忓€?!")
    public int pay_money;
    /**
     * 鎺ユ敹鏀粯涓績閫氱煡鐨刄RL锛岄渶缁欑粷瀵硅矾寰勶紝
     * 255瀛楃鍐呮牸寮忓:http://wap.tenpay.com/tenpay.asp锛?
     * 纭繚鏀粯涓績鑳介€氳繃浜掕仈缃戣闂鍦板潃
     */
    @URL(protocol="http",message="notify_url鏃犳晥鐨刄RL鍦板潃!")
    @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;
    /**
     * 鏀粯閫氶亾绫诲瀷锛?2锛氬井淇℃壂鐮侊紝22锛氭敮浠樺疂鎵爜锛?
     */
    @NotNull(message = "pay_channel涓嶈兘涓虹┖!")
    private int pay_channel;
    
    
    /**
     * getter/setter
     */
    
}

 

缂栧啓濡備笅testcase鏂规硶锛?/p>

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

run鏃舵姤閿欙細

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.BankCodeMapperTest.NotEmptyAttributeTest(BankCodeMapperTest.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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.BankCodeMapperTest.NotEmptyAttributeTest(BankCodeMapperTest.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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

 

 

瑙e喅鍔炴硶锛?/p>

  • 鎵惧悓浜嬪崗鍔╋紝灏濊瘯寮曞叆jstl锛屼笉濂忔晥銆?/li>
  • 灞炴€т笉鐢∟otEmpty娉ㄨВ锛屾敼鐢∟otBlank娉ㄨВ銆傦紙鍏跺疄锛屾垜浠墍闇€鐨勯獙璇侀」姝f槸NotBlank锛岃€岄潪NotEmpty锛?/li>
  • 鍙傝€僪ttp://blog.csdn.net/bsg_24/article/details/49494975锛屽皢hibernate validator鐨勭増鏈檷鍒?.3.1.Final銆?/li>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值