DEV Community

Discussion on: Framework-less REST API in Java

Collapse
 
michal1998 profile image
Michał

I have a generated XML data file. I want it to be available in Endpoint instead of "hello".

Thread Thread
 
michal1998 profile image
Michał

In conclusion, I want to type localhost: 8080 / XML and see the data from the XML file I have in the folder.

Thread Thread
 
piczmar_0 profile image
Marcin Piczkowski

Instead of hardcoded text just read the file content as bytes. The rest is the same.
I'm sure you can google out plenty of examples like this one:

 Path path = Paths.get("C:/temp/test.xml");
 byte[] data = Files.readAllBytes(path);
Enter fullscreen mode Exit fullscreen mode

Then :

exchange.getResponseHeaders().put("Content-Type", "text/xml");
exchange.sendResponseHeaders(200, data.length);
OutputStream output = exchange.getResponseBody();
 output.write(data);
 output.flush();
 exchange.close();

Enter fullscreen mode Exit fullscreen mode

Haven't checked it compiles but give it a try.