限制值以在soap Web 服务中发送

问题描述

我创建了一个soap web服务和一个xml验证器,我想知道是否有任何注释可以用来限制在任何字段中发送的值,例如:

   @XmlType
public class ContactoBean implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 6034293066688534650L;

    @XmlElement(required = true)
    private String emailContacto;
    
    @XmlElement(required = true)
    private String telfContacto;
    
    @XmlElement(required = false)
    private String faxContacto;

是否有任何注释可以在电话中使用正则表达式来验证它是否是正确的数字?或验证电子邮件的更正?

问候

解决方法

您是采用代码优先的方法还是契约优先的方法(根据 WSDL+XSD 文件生成类)?

如果是后者,您可以使用定义所需正则表达式的 xsd:patterns 扩展相关的 XSD 类型。

示例:

<xsd:element name="elementName">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="[^*+]"></xsd:pattern>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>    

XMLBeans 应该为您生成必要的类、getter 和 setter。

但是如果您直接通过 Java 代码创建 Web 服务,我假设您需要使用标准的 Java 方法来处理正则表达式。我发现以下 org.apache.xmlbeans.impl.regex.RegularExpression 描述 - 也许它有帮助:

A.标准方式

RegularExpression re = new RegularExpression(regex);
if (re.matches(text)) { ... }

B.捕获组

RegularExpression re = new RegularExpression(regex);
Match match = new Match();
if (re.matches(text,match)) {
    ... // You can refer captured texts with methods of the Match class.
}

不区分大小写的匹配

RegularExpression re = new RegularExpression(regex,"i");
if (re.matches(text) >= 0) { ...}

选项

You can specify options to RegularExpression(regex,options) or setPattern(regex,options). This options parameter consists of the following characters.

"i"
This option indicates case-insensitive matching.
"m"
^ and $ consider the EOL characters within the text.
"s"
. matches any one character.
"u"
Redefines \d \D \w \W \s \S \b \B \< \> as becoming to Unicode.
"w"
By this option,\b \B \< \> are processed with the method of 'Unicode Regular Expression Guidelines' Revision 4. When "w" and "u" are specified at the same time,\b \B \< \> are processed for the "w" option.
","
The parser treats a comma in a character class as a range separator. [a,b] matches a or,or b without this option. [a,b] matches a or b with this option.
"X"
By this option,the engine confoms to XML Schema: Regular Expression. The match() method does not do subsring matching but entire string matching.

来源:http://www.docjar.com/docs/api/org/apache/xmlbeans/impl/regex/RegularExpression.html

在我看来,我建议采用契约/WSDL-first 方法。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...