简体   繁体   中英

Java SOAP Message Content Type

I used maven wsimport and Java 1.7 generated the SOAP client side code. the WSDL was given by the customer, I believe its a .NET backed SOAP server.

After calling the service, I am keep getting a error like:

The server sent HTTP status code 415: Cannot process the message because the content type 'application/soap+xml; charset=utf-8; was not the expected type 'text/xml; charset=utf-8'.

I am trying to modify the MimeHeader in the SOAPMessage class though a customised handler (and I did register the handler to the chain correctly):

MimeHeaders mimeHeaders = msg.getMimeHeaders();
mimeHeaders.removeAllHeaders();
mimeHeaders.removeHeader("Content-Type");
mimeHeaders.addHeader(HttpHeaders.CONTENT_TYPE, "text/xml; charset=utf-8");

the above code seems doesn't change anything inside the mimiHeaders, the vector object seems to be immutable....

then I created the SOAP message manually

  MessageFactory newInstance = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
  MimeHeaders he = new MimeHeaders();
  he.addHeader("Content-Type", "text/xml; charset=utf-8");
  String ss = "<x:Envelope>xxxxx</x:Envelope>";
  InputStream in = new ByteArrayInputStream(ss.getBytes(StandardCharsets.UTF_8));

  SOAPMessage createMessage = newInstance.createMessage(he, in);
  createMessage.writeTo(System.out);  
  context.setMessage(createMessage);
  msg.saveChanges();

this time the mimeHeader and Content-Type on the SOAPMessage itself both set to text/xml; charset=utf-8

but the server still through the same error 415 application/soap+xml is not expected......

I understand the server seems follows SOAP 1.1 thats why its expecting text/xml... but when I created the factory I clearly specified the protocol to 1.1 and manually set the mimeHeader. I am confused really

Note: using SOAPUI or PostMan is absolutely fine, the problem only happens when using a wsimport generated java client

You should first try to spot the problem: on the client or on the server ? Therefore, I suggest you to add the following JVM argument in order to dump the HTTP request details.

-Dcom.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump=true

Using this, you should then see in the console something like:

---[HTTP request - http://localhost:8780/soap/MyService]---
Accept: text/xml
Authorization: Basic *****************==
Content-Type: text/xml; charset=utf-8
SOAPAction: "xxxxx"
User-Agent: JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">...

If you do see the correct value for the Content-Type error, no doubt: the error is located on the side of the service provider.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM