1

我已经在我的应用程序中实现了 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

4

1 回答 1

2

在您的 Web 服务定义中,您有一个定义消息的 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"它将<!-- optional -->在元素上添加注释

于 2012-06-03T20:46:01.320 回答