使用javax验证验证后返回自定义值

时间:2017-03-28 16:06:42

标签: java validation annotations

我正在尝试使用javax.validation包验证某些字段。我使用如下的注释,验证工作正常 -

[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.5.1.RELEASE:compile
[INFO] |  +- org.apache.tomcat:tomcat-jdbc:jar:8.5.11:compile
[INFO] |  |  \- org.apache.tomcat:tomcat-juli:jar:8.5.11:compile
[INFO] |  \- org.springframework:spring-jdbc:jar:4.3.6.RELEASE:compile
[INFO] |     +- org.springframework:spring-beans:jar:4.3.6.RELEASE:compile
[INFO] |     \- org.springframework:spring-tx:jar:4.3.6.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-security:jar:1.5.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.3.6.RELEASE:compile
[INFO] |  +- org.springframework.security:spring-security-config:jar:4.2.1.RELEASE:compile
[INFO] |  |  \- org.springframework.security:spring-security-core:jar:4.2.1.RELEASE:compile
[INFO] |  \- org.springframework.security:spring-security-web:jar:4.2.1.RELEASE:compile
[INFO] |     +- org.springframework:spring-expression:jar:4.3.6.RELEASE:compile
[INFO] |     \- org.springframework:spring-web:jar:4.3.6.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:1.5.1.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-web:jar:1.5.1.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.1.RELEASE:compile
[INFO] |  |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.11:compile
[INFO] |  |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.11:compile
[INFO] |  |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.11:compile
[INFO] |  |  +- org.hibernate:hibernate-validator:jar:5.3.4.Final:compile
[INFO] |  |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |  |  \- com.fasterxml:classmate:jar:1.3.3:compile
[INFO] |  |  \- org.springframework:spring-webmvc:jar:4.3.6.RELEASE:compile
[INFO] |  +- org.thymeleaf:thymeleaf-spring4:jar:2.1.5.RELEASE:compile
[INFO] |  \- nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:jar:1.4.0:compile
[INFO] |     \- org.codehaus.groovy:groovy:jar:2.4.7:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:1.5.1.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:1.5.1.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:4.3.6.RELEASE:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.17:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.1.RELEASE:compile
[INFO] |  +- ch.qos.logback:logback-classic:jar:1.1.9:compile
[INFO] |  |  \- ch.qos.logback:logback-core:jar:1.1.9:compile
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.22:compile
[INFO] |  +- org.slf4j:jul-to-slf4j:jar:1.7.22:compile
[INFO] |  \- org.slf4j:log4j-over-slf4j:jar:1.7.22:compile

验证返回一个ConstraintViolations列表。对于我的应用程序,该字段(如此处的productName)对应于电子表格中的列。我将列名存储在下面的枚举中 -

@NotEmpty(message = "Product Name is required.")
    @Pattern(regexp = ALPHA_NUMERIC, flags = Pattern.Flag.CASE_INSENSITIVE, message = "ProductName must be alphanumeric.")
    @Size(min = 100, max = 100, message = "ProductName may have maximum 100 characters.")
    private String productName;

如何让验证过程从此枚举中返回列值以及ConstraintViolation或作为其一部分?实现ConstraintViolation类是一种干净的方法吗?如果是,我认为这意味着添加另一个String,它存储列值以及getter和setter。在这种情况下,如何将此实现类与该字段上的注释相关联?

由于

0 个答案:

没有答案