DEV Community

Discussion on: Framework-less REST API in Java

 
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.