XML中的Hibernate验证程序约束 [英] Hibernate validator constraints in XML

查看:98
本文介绍了XML中的Hibernate验证程序约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xml文件来指定对传入请求bean的约束.我在validation-constraints.xml文件中执行以下操作:

I am using xml file to specify my constraints on an incoming request bean. I do this in a validation-constraints.xml file as follows:

<?xml version="1.0" encoding="UTF-8"?>
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
    xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
    <default-package>com.mathworks.internal.business.geoTypes
    </default-package>

    <bean class="GetStatesByCountryCodeRequest">
        <field name="countryCode">
            <constraint annotation="org.hibernate.validator.constraints.NotBlank" />

        </field>
    </bean>

</constraint-mappings>

我还需要countryCode仅2个字符长.我查看了hibernate提供的长度验证器,但是它需要一个最小值和一个最大值,在我的情况下,我猜应该是2和2.如何以XML方式指定长度约束?我找不到任何示例.

I also need the countryCode to be only 2 characters long. I looked at the length validator provided by hibernate but it requires a min and a max value which I am guessing in my case should be 2 and 2. How can I specify the length constraint in a XML way? I cannot find any examples for that.

推荐答案

我最终做了以下事情:

<bean class="GetStatesByCountryCodeRequest">
        <field name="countryCode">
            <constraint annotation="org.hibernate.validator.constraints.NotBlank" />
            <constraint annotation="org.hibernate.validator.constraints.Length">
                <element name="min">2</element>
                <element name="max">2</element>
            </constraint>

        </field>
    </bean>

这似乎已经解决了该问题.

This seems to have fixed the problem for now.

这篇关于XML中的Hibernate验证程序约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆