Java WebService可以';t处理SOAP主体没有命名空间前缀的请求
当客户端调用web服务时,如果不在SOAP正文中传递前缀,则我的web服务无法处理客户端的请求,如下所示:Java WebService可以';t处理SOAP主体没有命名空间前缀的请求,java,web-services,soap,namespaces,jax-ws,Java,Web Services,Soap,Namespaces,Jax Ws,当客户端调用web服务时,如果不在SOAP正文中传递前缀,则我的web服务无法处理客户端的请求,如下所示: <soap:Body> <GetPatientResultsRequest xmlns="http://urlA"> <PatientIdentification> <PersonCivilRegistrationIdentifier xmlns="http://UrlB"/> </PatientIdent
<soap:Body>
<GetPatientResultsRequest xmlns="http://urlA">
<PatientIdentification>
<PersonCivilRegistrationIdentifier xmlns="http://UrlB"/>
</PatientIdentification>
<Period>
<From>2012-05-26</From>
<To>2012-06-26</To>
</Period>
</GetPatientResultsRequest>
</soap:Body>
有人能告诉我该怎么做,以便我的web服务可以接受各种SOAP请求(即,正文中有前缀和没有前缀)
我正在使用JAX-WS(SOAP1.1)一个web服务定义了一个契约,您必须遵循该契约才能调用它。您发布的示例中只有一条消息与该契约匹配,因此一条有效,另一条无效 在第一条消息中,您定义了一个默认名称空间(因为包装器中的
xmlns
属性),并且所有未取消声明且没有前缀的元素都位于同一名称空间中,因为它们从父元素继承了名称空间
在第二条消息中,您有一个显式前缀声明,只有包装器在该名称空间中,其他元素不在名称空间中,并且不从父元素继承默认元素(因为缺少xmlns
属性)
正如我在开始时所说,web服务定义了一个契约修改客户端以发送正确的消息比更改服务以接受来自客户端的错误消息更有意义。
要控制元素的名称空间,您需要在web服务和客户端的JAX-WS注释上使用targetNamespace
值
下面是一个示例,以查看更改目标名称空间时代码和消息格式的差异。我将使用一个基本的WSDL:
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://tempuri.org"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://tempuri.org"
name="CalculatorWS">
<wsdl:types>
<xs:schema targetNamespace="http://tempuri.org">
<xs:element name="add" type="tns:add" />
<xs:element name="addInput" type="tns:addInput" />
<xs:element name="addResponse" type="tns:addResponse" />
<xs:element name="addOutput" type="tns:addOutput" />
<xs:complexType name="add">
<xs:sequence>
<xs:element name="addInput" type="tns:addInput" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="addInput">
<xs:sequence>
<xs:element name="a" type="xs:int" />
<xs:element name="b" type="xs:int" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="addResponse">
<xs:sequence>
<xs:element name="addOutput" type="tns:addOutput" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="addOutput">
<xs:sequence>
<xs:element name="result" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="add">
<wsdl:part name="parameters" element="tns:add" />
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part name="parameters" element="tns:addResponse" />
</wsdl:message>
<wsdl:portType name="CalculatorWS">
<wsdl:operation name="add">
<wsdl:input message="tns:add" />
<wsdl:output message="tns:addResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CalculatorWSPortBinding" type="tns:CalculatorWS">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<wsdl:operation name="add">
<soap:operation soapAction="http://tempuri.org/add" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CalculatorWSService">
<wsdl:port name="CalculatorWSPort" binding="tns:CalculatorWSPortBinding">
<soap:address location="http://localhost:8080/WebServices/CalculatorWS" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
此新WSDL将对应以下消息:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org">
<soapenv:Body>
<tem:add>
<addInput>
<a>?</a>
<b>?</b>
</addInput>
</tem:add>
</soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<add>
<addInput>
<a>?</a>
<b>?</b>
</addInput>
</add>
</soapenv:Body>
</soapenv:Envelope>
为此:
@WebService(name = "CalculatorWS", targetNamespace = "")
public interface CalculatorWS {
@WebMethod(action = "http://tempuri.org/add")
@WebResult(name = "addOutput", targetNamespace = "")
@RequestWrapper(localName = "add", targetNamespace = "", className = "your.pack.age.Add")
@ResponseWrapper(localName = "addResponse", targetNamespace = "", className = "your.pack.age.AddResponse")
public AddOutput add(
@WebParam(name = "addInput", targetNamespace = "")
AddInput addInput);
}
控制
targetNamespace
,您将控制消息的外观。您使用的客户端是什么?jaxws?你的两个例子是不同的。在第一种情况下,命名空间位于GetPatientResultsRequest
和PatientIdentification
、Period
、From
和To
元素上。在第二个例子中,它只在GetPatientResultsRequest
元素上,我面临同样的问题。请告诉我你是否能解决这个问题。。。
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
name="CalculatorWS">
<wsdl:types>
<xs:schema>
<xs:element name="add" type="add" />
<xs:element name="addInput" type="addInput" />
<xs:element name="addResponse" type="addResponse" />
<xs:element name="addOutput" type="addOutput" />
<xs:complexType name="add">
<xs:sequence>
<xs:element name="addInput" type="addInput" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="addInput">
<xs:sequence>
<xs:element name="a" type="xs:int" />
<xs:element name="b" type="xs:int" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="addResponse">
<xs:sequence>
<xs:element name="addOutput" type="addOutput" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="addOutput">
<xs:sequence>
<xs:element name="result" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="add">
<wsdl:part name="parameters" element="add" />
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part name="parameters" element="addResponse" />
</wsdl:message>
<wsdl:portType name="CalculatorWS">
<wsdl:operation name="add">
<wsdl:input message="add" />
<wsdl:output message="addResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CalculatorWSPortBinding" type="CalculatorWS">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<wsdl:operation name="add">
<soap:operation soapAction="http://tempuri.org/add" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CalculatorWSService">
<wsdl:port name="CalculatorWSPort" binding="CalculatorWSPortBinding">
<soap:address location="http://localhost:8080/WebServices/CalculatorWS" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<add>
<addInput>
<a>?</a>
<b>?</b>
</addInput>
</add>
</soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<addResponse>
<addOutput>
<result>?</result>
</addOutput>
</addResponse>
</soapenv:Body>
</soapenv:Envelope>
@WebService(name = "CalculatorWS", targetNamespace = "http://tempuri.org")
public interface CalculatorWS {
@WebMethod(action = "http://tempuri.org/add")
@WebResult(name = "addOutput", targetNamespace = "")
@RequestWrapper(localName = "add", targetNamespace = "http://tempuri.org", className = "your.pack.age.Add")
@ResponseWrapper(localName = "addResponse", targetNamespace = "http://tempuri.org", className = "your.pack.age.AddResponse")
public AddOutput add(
@WebParam(name = "addInput", targetNamespace = "")
AddInput addInput);
}
@WebService(name = "CalculatorWS", targetNamespace = "")
public interface CalculatorWS {
@WebMethod(action = "http://tempuri.org/add")
@WebResult(name = "addOutput", targetNamespace = "")
@RequestWrapper(localName = "add", targetNamespace = "", className = "your.pack.age.Add")
@ResponseWrapper(localName = "addResponse", targetNamespace = "", className = "your.pack.age.AddResponse")
public AddOutput add(
@WebParam(name = "addInput", targetNamespace = "")
AddInput addInput);
}