Skip to content

Instantly share code, notes, and snippets.

@judoole
Last active October 6, 2015 09:12
Show Gist options
  • Select an option

  • Save judoole/7dc1203d282b27a40bbc to your computer and use it in GitHub Desktop.

Select an option

Save judoole/7dc1203d282b27a40bbc to your computer and use it in GitHub Desktop.
Unmarshal a jax ws response to object
//Using Spring to create a reader. Should be easy to write this yourself.
//file is the xml response you got stripped of SoapEnvelope and SoapBody.
def classpathResource: ClassPathResource = new ClassPathResource(file)
def inputStream: InputStream = classpathResource.getInputStream()
def xmlInputFactory: XMLInputFactory = XMLInputFactory.newInstance()
def xmlReader: XMLStreamReader = xmlInputFactory.createXMLStreamReader(inputStream)
//ObjectFactory you can find in your jax ws generated code
val instance: JAXBContext = JAXBContext.newInstance(classOf[ObjectFactory])
val unmarshaller: Unmarshaller = instance.createUnmarshaller()
//Unmarshal using JAXB, cast to JAXBElement.
val response: JAXBElement[TheClassYouWanToUnmarshal] = unmarshaller.unmarshal(xmlReader).asInstanceOf[JAXBElement[TheClassYouWanToUnmarshal]]
//Get the value. If no exceptions, use it in a test.
val value: TheClassYouWanToUnmarshal = response.getValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment