【问题标题】:Java Web Services Indicating Requirement Levels of Attributes指示属性要求级别的 Java Web 服务
【发布时间】:2012-06-01 08:14:09
【问题描述】:

我已经在我的应用程序中实现了 Web 服务。当我想通过带有 Soap UI 的 SOA 上的 Web 服务将对象发送到我的服务器时,我会得到这样的结果:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="http://impl.arg.lou.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <impl:addStudent>
         <!--Optional:-->
         <student>
            <!--Optional:-->
            <name>?</name>
         </student>
      </impl:addStudent>
   </soapenv:Body>
</soapenv:Envelope>

我不希望名称字段是可选的,它应该是必填字段。我该怎么做?

我使用 Apache cxf。

要求级别链接:http://www.ietf.org/rfc/rfc2119.txt

【问题讨论】:

  • 您应该更改问题的标题:您要求对 Web 服务消息添加限制,对吗?

标签: java xml web-services


【解决方案1】:

在您的 Web 服务定义中,您有一个 type 部分,用于定义消息的 XML 模式。在该定义中,您可以设置多个约束,例如“必填字段”。

示例:

<wsdl:definitions name="myService" ...>
  <wsdl:types>
    <xs:schema version="1.0" targetNamespace="myNameSpace" xmlns:tns="myNameSpace">
      <xs:complexType name="studentType">
        <xs:attribute name="name" type="xs:string" use="required" />
      </xs:complexType>
      <xs:element name="student" nillable="false" type="tns:studentType"/>
    </xs:schema>
  </wsdl:types>
  ...
 </wsdl:definitions>

现在,如果您没有像这样在 apache CXF 配置中激活模式验证,则不会在服务器端进行验证:

<jaxws:endpoint id="myService" ...>
  <jaxws:properties>
    <entry key="schema-validation-enabled" value="true" />
    ...
  </jaxws:properties>
  ...
</jaxws:endpoint>

PS:SOAP UI 使用目标 Web 服务的 XML 模式来生成默认请求。如果 XML 模式没有 use="required"nillable="false",它将在元素上添加 &lt;!-- optional --&gt; 注释

【讨论】:

  • 我可以在 Java 类中定义变量之前使用注解吗?
  • 如果您使用“第一个代码”方法使用 JAXWS 和 JAXB,则可以使用 JAXB 注释,如 @XmlElement(nillable=false)@XmlAttribute(required=false)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 2011-09-02
  • 2014-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多