LPD
LPD

Reputation: 2883

How to pass JAXB marshalled data over sockets

Am using HTTPServer for my web service. My web server has an object which is converted to XML by using JAXB parser.

How can I send the response using the HTTPExchange object?

Thanks.

Upvotes: 2

Views: 378

Answers (2)

bdoughan
bdoughan

Reputation: 149037

You could do the following:

httpExchange.sendResponseHeaders(rCode, 0);
OutputStream outputStream = httpExchange.getResonseBody();
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.marshal(myPojo, outputStream);
outputStream.close();

Upvotes: 1

f1sh
f1sh

Reputation: 11943

Use the getResponseBody() method to obtain an OutputStream. Then use the stream's write(byte[] b) method to write to the stream.

String strXml = ... ; //your xml
OutputStream stream = exchange.getResponseBody();
stream.write(strXml.getBytes(Charset.forName("UTF-8")));
stream.close();

This will write the xml into the server's response using a given encoding, in this case it's UTF-8.

Upvotes: 0

Related Questions