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/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
瑙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>