user3364825
user3364825

Reputation: 1551

Convert SOAP XML to Java objects

I have a complete SOAP message as a String. I have some corresponding Java classes generated from a WSDL using wsimport. How can I convert the SOAP message into a Java object?

Upvotes: 1

Views: 6627

Answers (1)

user3364825
user3364825

Reputation: 1551

This is what I ended up with (example mapping to a Java type Thingy):

MessageFactory messageFactory = MessageFactory.newInstance();
ByteArrayInputStream soapStringStream = new ByteArrayInputStream(soapString.getBytes(Charset.forName("UTF-8")));
SOAPMessage soapMessage = messageFactory.createMessage(new MimeHeaders(), soapStringStream);
Unmarshaller unmarshaller = JAXBContext.newInstance(Thingy.class).createUnmarshaller();
Document bodyDoc = soapMessage.getSOAPBody().extractContentAsDocument();
Thingy request = (Thingy) unmarshaller.unmarshal(bodyDoc);

Upvotes: 2

Related Questions