DEV Community

Framework-less REST API in Java

Marcin Piczkowski on December 29, 2018

Java ecosystem is packed with frameworks and libraries. For sure not as many as in JavaScript world and they don't get old as quickly too but st...
Collapse
 
billoneil profile image
Bill O'Neil

I'm writing a Java blog that doesn't use a framework and all of the code is available on github. I do use Undertow as the web server which gives me a lot of the HTTP parts. stubbornjava.com/

Collapse
 
piczmar_0 profile image
Marcin Piczkowski

Thanks Bill, I'll have a look.

Collapse
 
at198x profile image
at198x

Thank you. I am looking for a way to implement REST API without Spring involved so Kubernetes can check my liveness probe, and your post work perfectly.

Collapse
 
michal1998 profile image
Michał

Can I use an xml file instead of "hello"?

Collapse
 
piczmar_0 profile image
Marcin Piczkowski

Can you explain what you mean exactly?

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.

Collapse
 
kareemradwan profile image
Kareem Radwan

it's very nice

Collapse
 
piczmar_0 profile image
Marcin Piczkowski

Thank you.

Collapse
 
c62eu19 profile image
Stan Zajdel

You can simplify this even further by simply using a servlet which handles much of what you have coded in your example.

Collapse
 
piczmar_0 profile image
Marcin Piczkowski

Probably, but I would not like to develop my own servlet container from scratch when we have plenty of them already. I would use Tomcat or Jetty or other if I went this route. The purpose was to make sth simple from ground up, but the experiment shows it's not that simple after all when the API grows, cause eventually we would come up with reinventing a wheel. My feelings, weather to use it in real app, or stick to well known frameworks like Spring are mixed. Still, I think it's good to know underlying techniques that the frameworks base on.

Collapse
 
kimf profile image
K

Super useful article, thanks!

Collapse
 
piczmar_0 profile image
Marcin Piczkowski

Hi, thanks. Let me know if you find some parts of it applicable in production ready app. I'm curious.