Client avec javax.xml.ws & SOAP 1.2 : double invocation ou UnsupportedMediaException
Bonjour,
J'ai créé un client JAX-WS RI invoquant dynamiquement un service web et utilisant SOAP 1.2 :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| package client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
public class DispatchClient {
private static String EPR = "http://localhost:8080/axis2/services/Service";
private static String TNS = "http://service";
public static void main(String args[]) {
QName serviceQName = new QName(TNS, "Service");
QName portQName = new QName(TNS, "ServiceHttpSoap12Endpoint");
try {
Service service = Service.create(new URL(EPR), serviceQName);
try {
Dispatch<Source> dispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
StreamSource request = new StreamSource("http://localhost:8080/axis2/services/Service/sayHello?name=Christophe");
Source response = dispatch.invoke(request);
Transformer copier = TransformerFactory.newInstance().newTransformer();
copier.transform(response, new StreamResult(System.out));
} catch(TransformerConfigurationException e) {
e.printStackTrace();
} catch(TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch(TransformerException e) {
e.printStackTrace();
}
System.out.println();
try {
Dispatch<SOAPMessage> dispatch = service.createDispatch(portQName, SOAPMessage.class, Service.Mode.MESSAGE);
SOAPMessage request = MessageFactory.newInstance().createMessage();
SOAPEnvelope envelope = request.getSOAPPart().getEnvelope();
request.getSOAPBody().addBodyElement(envelope.createName("sayHello", "ns", TNS)).addChildElement(envelope.createName("name")).addTextNode("Christophe");
SOAPMessage response = dispatch.invoke(request);
System.out.println(response.getSOAPPart().getEnvelope().getTextContent());
} catch(SOAPException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
} |
Si j'utilise le mode Service.Mode.PAYLOAD, le service web semble invoqué deux fois, c'est-à-dire qu'au lieu de retourner "Hello Christophe !", il retourne "Hello Hello Christophe ! !".
Voici la réponse SOAP obtenue :
Code:
1 2 3 4 5 6 7 8
| <?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<Service:sayHello xmlns:Service="http://localhost:8080/Service">
<Service:name>Hello Hello Christophe ! !</Service:name>
</Service:sayHello>
</soapenv:Body>
</soapenv:Envelope> |
Si par contre j'utilise le mode Service.Mode.MESSAGE, j'obtiens l'exception suivante :
Citation:
Exception in thread "main" com.sun.xml.internal.ws.server.UnsupportedMediaException: Unsupported Content-Type: text/html;charset=iso-8859-1 Supported ones are: [application/soap+xml]
at com.sun.xml.internal.ws.encoding.StreamSOAPCodec.decode(Unknown Source)
at com.sun.xml.internal.ws.encoding.SOAPBindingCodec.decode(Unknown Source)
at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(Unknown Source)
at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.processRequest(Unknown Source)
at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Unknown Source)
at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Unknown Source)
at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Unknown Source)
at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Unknown Source)
at com.sun.xml.internal.ws.client.Stub.process(Unknown Source)
at com.sun.xml.internal.ws.client.dispatch.DispatchImpl.doInvoke(Unknown Source)
at com.sun.xml.internal.ws.client.dispatch.DispatchImpl.invoke(Unknown Source)
at client.DispatchClient.main(DispatchClient.java:58)
Voici le fichier WSDL du service :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| <?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://service" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://service">
<wsdl:documentation>Service</wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service">
<xs:complexType name="Exception">
<xs:sequence>
<xs:element minOccurs="0" name="Exception" nillable="true" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
<xs:element name="XMLStreamException">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="XMLStreamException" nillable="true" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="sayHello">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="sayHello" nillable="true" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="sayHelloResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHelloRequest">
<wsdl:part name="parameters" element="ns:sayHello"/>
</wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="ns:sayHelloResponse"/>
</wsdl:message>
<wsdl:message name="XMLStreamException">
<wsdl:part name="parameters" element="ns:XMLStreamException"/>
</wsdl:message>
<wsdl:portType name="ServicePortType">
<wsdl:operation name="sayHello">
<wsdl:input message="ns:sayHelloRequest" wsaw:Action="urn:sayHello"/>
<wsdl:output message="ns:sayHelloResponse" wsaw:Action="urn:sayHelloResponse"/>
<wsdl:fault message="ns:XMLStreamException" name="XMLStreamException" wsaw:Action="urn:sayHelloXMLStreamException"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ServiceSoap11Binding" type="ns:ServicePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="urn:sayHello" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
<wsdl:fault name="XMLStreamException">
<soap:fault use="literal" name="XMLStreamException"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="ServiceSoap12Binding" type="ns:ServicePortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="sayHello">
<soap12:operation soapAction="urn:sayHello" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
<wsdl:fault name="XMLStreamException">
<soap12:fault use="literal" name="XMLStreamException"/>
</wsdl:fault>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="ServiceHttpBinding" type="ns:ServicePortType">
<http:binding verb="POST"/>
<wsdl:operation name="sayHello">
<http:operation location="Service/sayHello"/>
<wsdl:input>
<mime:content type="text/xml" part="sayHello"/>
</wsdl:input>
<wsdl:output>
<mime:content type="text/xml" part="sayHello"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Service">
<wsdl:port name="ServiceHttpSoap11Endpoint" binding="ns:ServiceSoap11Binding">
<soap:address location="http://10.63.0.52:8080/axis2/services/Service.ServiceHttpSoap11Endpoint"/>
</wsdl:port>
<wsdl:port name="ServiceHttpSoap12Endpoint" binding="ns:ServiceSoap12Binding">
<soap12:address location="http://10.63.0.52:8080/axis2/services/Service.ServiceHttpSoap12Endpoint"/>
</wsdl:port>
<wsdl:port name="ServiceHttpEndpoint" binding="ns:ServiceHttpBinding">
<http:address location="http://10.63.0.52:8080/axis2/services/Service.ServiceHttpEndpoint"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions> |
Merci à vous.