Here is the second part of my Java EE tutorial series. Last week I took a look at setting up a Tomcat server and creating my first project with Eclipse. If you missed it, take a look here and make sure you are familiar with the basics.
The next step would be to create a RESTful API so our future web application will be able to easily talk to our backend. I want to start from the scratch to understand the challenges, so instead of using one of the many existing REST frameworks, I decided to implement myself a REST API using a simple servlet like the one I created last week.
I'm going to create an API that will perform the following tasks and look as follow:
- Get the list of models from our database or get a specific model based on its id:
GET /models
GET /models/{id}
- Add a new model to the database:
POST /models
- Update an existing model in database:
PUT /model/{id}
- Remove an existing model from the database:
DELETE /models/{id}
A model will be a very basic structure containing info to load into the viewer:
So let's get started...
1/ First step is to add a new servlet to our project: right-click your project > New > Servlet. I name it "Models". By default the servlet will be mapped to the following url: /AppName/Models. We need to allow more routes to be redirected to that servlet, so edit the @WebServlet decorator as follow, so every request starting with /model will be redirected to that servlet:
2/ My REST API is going to use JSON as data format, there are some built-in classes to handle serialization and deserialization, however I'm familiar with a powerful library from Google: GSON which I used previously in some Android projects. So download the .jar from there and add it to your project: Right-click > Properties > Java Build Path > Add External Jars > browser to gson.jar.
Once you've done that, there is another trick: as your project now depends on a third party library, you need to include that in your deployment package, so it will be available when it runs on the server. No worries: Still from project's Properties > Deployment Assembly > Add > Java Build Path Entries > select gson.jar > done!
3/ Implement doGet, doPut, doPost and doDelete methods in your servlet. In the example below I'm using a simple hashmap to "simulate" a backend database, so it can keep track of my records, this is obviously not a real world scenario but it serves the purpose of that tutorial.Here is a very naive implementation of my API:
4/ Finally run the servlet from Eclipse and you can use a tool like Postman to test your API:To wrap it up: we have a working RESTful API run by a servlet, however I found it pretty laborious to parse the url to extract parameters like the model Id although it's very basic.
If we have to add some more complexity to it, let's say for example GET /models/{id}/components/{nodeId}/material to find material attached to a specific component of a specific model, then it will quickly become very messy.
Next week I will pick up a popular REST framework and take a look how much easier it can be to achieve the same, stay tuned ...
Comments
You can follow this conversation by subscribing to the comment feed for this post.