【问题标题】:Restrict values to send in a soap web service限制值以在肥皂网络服务中发送
【发布时间】:2021-02-05 08:43:50
【问题描述】:

我已经创建了一个肥皂网络服务和一个 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;

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

问候

【问题讨论】:

    标签: xml soap xml-parsing xml-validation


    【解决方案1】:

    您是采用代码优先方法还是契约优先方法(基于 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 方法。

    【讨论】:

    • 您好,感谢您的回答。那件事,我先做代码,我会用注释来做,而不是编码所有的验证,这是一个例子,但服务很大..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 2017-01-16
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多