XML response synchronization with JAXB

I want to read the response from calling a web service other than wsdl using JAXB. I send a POST request using HttpURLConnection and get a response. My question is to make an XML document from a response stream, and then use jaxb to create Java objects? Or is it possible to use jaxb "on the fly" with a stream of answers? It will be a web application and I will not be able to store the generated XML document anywhere, so if I need to create an xml document, how do I store it to use jaxb, if I can not do jaxb on the fly

+5
source share
2 answers

Unmarshaller.unmarshal can accept an InputStream, which eliminates the need to parse it in an XML document.

+3
source

Here is an example:

String uri =
    "http://localhost:8080/CustomerService/rest/customers/1";
URL url = new URL(uri);
HttpURLConnection connection =
    (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");

JAXBContext jc = JAXBContext.newInstance(Customer.class);
InputStream xml = connection.getInputStream();
Customer customer =
    (Customer) jc.createUnmarshaller().unmarshal(xml);

connection.disconnect();

For more information see

+7
source

All Articles