Java SOAP message content type

I generated soap client code using maven wsimport and Java 1.7

After calling the service, I continue to receive the following errors:

I tried to modify the mimeheader in the soapmessage class through a custom handler (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 doesn't seem to change anything in mimiheaders, and the vector object seems 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 of soapmessage itself are set to text / XML; Character set = UTF-8

But the server still passes the same error 415. Application / soap XML does not expect

I understand that the server seems to follow soap 1.1, which is why it expects text / XML... But when I create the factory, I explicitly specify the protocol as 1.1 and manually set the mimeheader I'm really confused

Note: there is absolutely no problem using soapUI or postman. Problems only occur when using the Java client generated by wsimport

Solution

You should first try to find the problem: on the client or server?

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

Using this, you should see something similar in the console:

---[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 of the content type error, there is no doubt that the error is on the service provider's side

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>