1. Introduction
NanoHTTPD is an open-source, lightweight, web server written in Java.
In this tutorial, we’ll create a few REST APIs to explore its features.
2. Project Setup
Let’s add the NanoHTTPD core dependency to our pom.xml:
<dependency> <groupId>org.nanohttpd</groupId> <artifactId>nanohttpd</artifactId> <version>2.3.1</version> </dependency>
To create a simple server, we need to extend NanoHTTPD and override its serve method:
public class App extends NanoHTTPD { public App() throws IOException { super(8080); start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); } public static void main(String[] args ) throws IOException { new App(); } @Override public Response serve(IHTTPSession session) { return newFixedLengthResponse("Hello world"); } }
We defined our running port as 8080 and server to work as a daemon (no read timeout).
Once we’ll start the application, the URL http://localhost:8080/ will return the Hello world message. We’re using NanoHTTPD#newFixedLengthResponse method as a convenient way of building a NanoHTTPD.Response object.
Let’s try our project with cURL:
> curl 'http://localhost:8080/' Hello world
3. REST API
In the way of HTTP methods, NanoHTTPD allows GET, POST, PUT, DELETE, HEAD, TRACE, and several others.
Simply put, we can find supported HTTP verbs via the method enum. Let’s see how this plays out.
3.1. HTTP GET
First, let’s take a look at GET. Say, for example, that we want to return content only when the application receives a GET request.
Unlike Java Servlet containers, we don’t have a doGet method available – instead, we just check the value via getMethod:
@Override public Response serve(IHTTPSession session) { if (session.getMethod() == Method.GET) { String itemIdRequestParameter = session.getParameters().get("itemId").get(0); return newFixedLengthResponse("Requested itemId = " + itemIdRequestParameter); } return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "The requested resource does not exist"); }
That was pretty simple, right? Let’s run a quick test by curling our new endpoint and see that the request parameter itemId is read correctly:
> curl 'http://localhost:8080/?itemId=23Bk8' Requested itemId = 23Bk8
3.2. HTTP POST
We previously reacted to a GET and read a parameter from the URL.
In order to cover the two most popular HTTP methods, it’s time for us to handle a POST (and thus read the request body):
@Override public Response serve(IHTTPSession session) { if (session.getMethod() == Method.POST) { try { session.parseBody(new HashMap<>()); String requestBody = session.getQueryParameterString(); return newFixedLengthResponse("Request body = " + requestBody); } catch (IOException | ResponseException e) { // handle } } return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "The requested resource does not exist"); }
We’ll include a body in our cURL command:
> curl -X POST -d 'deliveryAddress=Washington nr 4&quantity=5''http://localhost:8080/' Request body = deliveryAddress=Washington nr 4&quantity=5
The remaining HTTP methods are very similar in nature, so we’ll skip those.
4. Cross-Origin Resource Sharing
@Override public Response serve(IHTTPSession session) { Response response = newFixedLengthResponse("Hello world"); response.addHeader("Access-Control-Allow-Origin", "*"); return response; }
Now when we cURL, we’ll get our CORS header back:
> curl -v 'http://localhost:8080' HTTP/1.1 200 OK Content-Type: text/html Date: Thu, 13 Jun 2019 03:58:14 GMT Access-Control-Allow-Origin: * Connection: keep-alive Content-Length: 11 Hello world
5. File Upload
NanoHTTPD has a separate dependency for file uploads, so let’s add it to our project:
<dependency> <groupId>org.nanohttpd</groupId> <artifactId>nanohttpd-apache-fileupload</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency>
Please note that the servlet-api dependency is also needed (otherwise we’ll get a compilation error).
What NanoHTTPD exposes is a class called NanoFileUpload:
@Override public Response serve(IHTTPSession session) { try { List<FileItem> files = new NanoFileUpload(new DiskFileItemFactory()).parseRequest(session); int uploadedCount = 0; for (FileItem file : files) { try { String fileName = file.getName(); byte[] fileContent = file.get(); Files.write(Paths.get(fileName), fileContent); uploadedCount++; } catch (Exception exception) { // handle } } return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Uploaded files " + uploadedCount + " out of " + files.size()); } catch (IOException | FileUploadException e) { throw new IllegalArgumentException("Could not handle files from API request", e); } return newFixedLengthResponse( Response.Status.BAD_REQUEST, MIME_PLAINTEXT, "Error when uploading"); }
Hey, let’s try it out:
> curl -F 'filename=@/pathToFile.txt' 'http://localhost:8080' Uploaded files: 1
6. Multiple Routes
Firstly, let’s add the required dependency for nanolets:
<dependency> <groupId>org.nanohttpd</groupId> <artifactId>nanohttpd-nanolets</artifactId> <version>2.3.1</version> </dependency>
And now we’ll extend our main class using the RouterNanoHTTPD, define our running port and have the server run as a daemon.
The addMappings method is where we’ll define our handlers:
public class MultipleRoutesExample extends RouterNanoHTTPD { public MultipleRoutesExample() throws IOException { super(8080); addMappings(); start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); } @Override public void addMappings() { // todo fill in the routes } }
The next step is to define our addMappings method. Let’s define a few handlers.
The first one is an IndexHandler class to “/” path. This class comes with the NanoHTTPD library and returns by default a Hello World message. We can override the getText method when we want a different response:
addRoute("/", IndexHandler.class); // inside addMappings method
And to test our new route we can do:
> curl 'http://localhost:8080' <html><body><h2>Hello world!</h3></body></html>
Secondly, let’s create a new UserHandler class which extends the existing DefaultHandler. The route for it will be /users. Here we played around with the text, MIME type, and the status code returned:
public static class UserHandler extends DefaultHandler { @Override public String getText() { return "UserA, UserB, UserC"; } @Override public String getMimeType() { return MIME_PLAINTEXT; } @Override public Response.IStatus getStatus() { return Response.Status.OK; } }
To call this route we’ll issue a cURL command again:
> curl -X POST 'http://localhost:8080/users' UserA, UserB, UserC
Finally, we can explore the GeneralHandler with a new StoreHandler class. We modified the returned message to include the storeId section of the URL.
public static class StoreHandler extends GeneralHandler { @Override public Response get( UriResource uriResource, Map<String, String> urlParams, IHTTPSession session) { return newFixedLengthResponse("Retrieving store for id = " + urlParams.get("storeId")); } }
Let’s check our new API:
> curl 'http://localhost:8080/stores/123' Retrieving store for id = 123
7. HTTPS
In order to use the HTTPS, we’ll need a certificate. Please refer to our article on SSL for more in-depth information.
We could use a service like Let’s Encrypt or we can simply generate a self-signed certificate as follows:
> keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048 -ext SAN=DNS:localhost,IP:127.0.0.1 -validity 9999
Next, we’d copy this keystore.jks to a location on our classpath, like say the src/main/resources folder of a Maven project.
After that, we can reference it in a call to NanoHTTPD#makeSSLSocketFactory:
public class HttpsExample extends NanoHTTPD { public HttpsExample() throws IOException { super(8080); makeSecure(NanoHTTPD.makeSSLSocketFactory( "/keystore.jks", "password".toCharArray()), null); start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); } // main and serve methods }
And now we can try it out. Please notice the use of the —insecure parameter, because cURL won’t be able to verify our self-signed certificate by default:
> curl --insecure 'https://localhost:8443' HTTPS call is a success
8. WebSockets
NanoHTTPD supports WebSockets.
Let’s create the simplest implementation of a WebSocket. For this, we’ll need to extend the NanoWSD class. We’ll also need to add the NanoHTTPD dependency for WebSocket:
<dependency> <groupId>org.nanohttpd</groupId> <artifactId>nanohttpd-websocket</artifactId> <version>2.3.1</version> </dependency>
For our implementation, we’ll just reply with a simple text payload:
public class WsdExample extends NanoWSD { public WsdExample() throws IOException { super(8080); start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); } public static void main(String[] args) throws IOException { new WsdExample(); } @Override protected WebSocket openWebSocket(IHTTPSession ihttpSession) { return new WsdSocket(ihttpSession); } private static class WsdSocket extends WebSocket { public WsdSocket(IHTTPSession handshakeRequest) { super(handshakeRequest); } //override onOpen, onClose, onPong and onException methods @Override protected void onMessage(WebSocketFrame webSocketFrame) { try { send(webSocketFrame.getTextPayload() + " to you"); } catch (IOException e) { // handle } } } }
Instead of cURL this time, we’ll use wscat:
> wscat -c localhost:8080 hello hello to you bye bye to you
9. Conclusion
To sum it up, we’ve created a project that uses the NanoHTTPD library. Next, we defined RESTful APIs and explored more HTTP related functionalities. In the end, we also implemented a WebSocket.
The implementation of all these snippets is available over on GitHub.