SOAP WS - 使@WebParam可选

amo*_*fis 15 java soap web-services jax-ws jaxb

我有一个非常简单的方法,我通过JAX-WS注释在WS API中使用它:

@WebMethod
public MyResponse sendSingle2(
    @WebParam(name="username") String username,
    @WebParam(name="password") String password,
    @WebParam(name="newParam") String newParam) {
        // the code
    }
Run Code Online (Sandbox Code Playgroud)

现在我希望newParam是可选的.我的意思是我希望该方法不仅可以在传递的xml中参数为空时工作:

<ws:sendSingle2>
    <username>user</username>
    <password>pass</password>
    <newParam></newParam>
</ws:sendSingle2>
Run Code Online (Sandbox Code Playgroud)

但是当它不存在时:

<ws:sendSingle2>
    <username>user</username>
    <password>pass</password>
</ws:sendSingle2>
Run Code Online (Sandbox Code Playgroud)

我需要它不要破坏现有的API,它在没有新参数的情况下工作.

Rom*_*ner 16

@WebParam将消息部分映射到参数,而部分不能是可选的.请参阅WSDL中的可选消息部分.因此,简短的回答是,正是你所要求的无法完成.但是,如果您可以重构此方法,则可以使用下面描述的方法之一.

通常,参数的可选性通过模式设置minOccurs=0.此外,您可以在架构中定义一个Request参数,而不是使用多个参数,您可以将其定义为您的参数WebMethod.可选项现在封装在参数中,并且对于带或不带可选参数的调用,都会调用相同的方法.

我更喜欢先定义合约,而不是依赖自动生成的文件.一旦你弄清楚XSD,SOAP和WSDL如何一起玩,你几乎不想再使用基于注释/代码优先的定义了,因为你反过来更灵活.

代码示例:

<xs:schema
    targetNamespace="http://your.namespace.com"
    xmlns:tns="http://your.namespace.com"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFromDefault="qualified"
    attributeFromDefault="qualified">

...

<xs:element name="MyRequest" type="tns:MyRequestType" />
<xs:element name="MyResponse" type="tns:MyResponseType" />

<xs:complexType name"MyRequestType">
    <xs:sequence>
        <xs:element name="username" type="xs:string" minOccurs="1" maxOccurs="1" />
        <xs:element name="password" type="xs:string" minOccurs="1" maxOccurs="1" />
        <xs:element name="newParam" type="xs:string" minOccurs="0" maxOccurs="1" />
    </xs:sequence>
</xs:complexType>

...

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

在您的WSDL文件中,您可以像这样定义消息:

<wsdl:definitions
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:msg="http://your.namespace.com"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    targetNamespace="http://your.namespace.com">

    <wsdl:types>
        <xs:schema>
            <!-- either import the externalized schema -->
            <xs:import namespace="http://your.namespace.com"
                       schemaLocation="someDir/yourMessageSchema.xsd" />
        </xs:schema>
        <!-- or define the schema within the WSDL - just copy the schema here -->
        <xs:schema
            targetNamespace="http://your.namespace.com"
            xmlns:tns="http://your.namespace.com"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            elementFromDefault="qualified"
            attributeFromDefault="qualified">
                ...
        </xs:schema>
    </wsdl:types>

    ...

    <wsdl:message name="sendSingle2Request">
        <wsdl:part name="in" element="msg:MyRequest" />
    </wsdl:message>

    <wsdl:message name="sendSingle2Response">
        <wsdl:part name="out" element="msg:MyResponse" />
    </wsdl:message>

    ...

    <wsdl:portType name="YourServiceEndpoint">
        <wsdl:operation name="sendSingle2">
            <wsdl:input message="tns:sendSingle2Request" />
            <wsdl:output message="tns:sendSingle2Response" />
        </wsdl:operation>
        ...
    </wsdl:portType>

    <wsdl:binding name="YourServiceBinding" type="YourServiceEndpoint">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name=""sendSingle2">
            <soap:operation soapAction="http://your.namespace.com/SendSingle2" style="document" />
            <wsdl:input>
                <soap:body parts="in" use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body parts="out" use="literal" />
            </wsdl:output>
        </wsdl:operation>
        ...
    </wsdl:binding>

    <wsdl:service name="YourService">
        <wsdl:port name="YourServicePort binding="tns:YourServiceBinding">
            <soap:address location="http://your.server:port/path/to/the/service" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>
Run Code Online (Sandbox Code Playgroud)

这里的WSDL契约定义了使用样式:document/literal并且在模式的帮助下,实际的SOAP消息将document/literal wrapped是WS-I兼容的.

因此,您的方法将更改为public MyResponse sendSinge2(MyRequest request),request现在封装username,passowrdnewParam.如果newParam没有发送SOAP请求它只会返回null,所以在你使用它之前最好先检查一下.

如果您坚持使用代码优先方法,则需要首先定义您的MyRequest类,将其用作请求参数而不是那些2或3值.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "MyRequest", namespace="http://your.namespace.com")
public class MyRequest implements Serializable
{
   @XmlElement(name = "username", required = true)
   protected String username;
   @XmlElement(name = "password", required = true)
   protected String password;
   @XmlElement(name = "newParam", required = false)
   protected String newParam;
   ...
}
Run Code Online (Sandbox Code Playgroud)

MyResult如果你还没有这样做,也应该这样做.web方法现在可能看起来像这样:

@WebMethod(operationName = "sendSingle2")
@WebResult(name = "sendSingle2Response", targetNamespace = "http://your.namespace.com")
public MyResult sendSingle2(@WebParam(name = "sendSingle2Request") MyRequest request)
{
   ...
}
Run Code Online (Sandbox Code Playgroud)

再次,request封装3个参数,您应该检查可选参数是否为空.

HTH