Skip to content

Instantly share code, notes, and snippets.

@leefsmp
Last active February 11, 2024 19:52
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save leefsmp/b4c089734852c793cf85 to your computer and use it in GitHub Desktop.
Save leefsmp/b4c089734852c793cf85 to your computer and use it in GitHub Desktop.
Basic RESTful API from a Java servlet
package com.autodesk.adn.viewanddata;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
/**
* Servlet implementation class Models
*/
@WebServlet("/models/*")
public class Models extends HttpServlet {
private static final long serialVersionUID = 1L;
class Model{
public String name;
public String urn;
public String id;
public Model(String _id, String _name, String _urn) {
name = _name;
urn = _urn;
id = _id;
}
}
private Gson _gson = null;
private HashMap<String, Model> _modelsDb = new HashMap<>();
//Adds some default models to our db
public Models() {
super();
_gson = new Gson();
String id1 = UUID.randomUUID().toString();
String id2 = UUID.randomUUID().toString();
String id3 = UUID.randomUUID().toString();
_modelsDb.put(id1,
new Model(
id1,
"Engine",
"... base 64 URN ..."));
_modelsDb.put(id2,
new Model(
id2,
"Hairdryer",
"... base 64 URN ..."));
_modelsDb.put(id3,
new Model(
id3,
"Plane Engine",
"... base 64 URN ..."));
}
//a utility method to send object
//as JSON response
private void sendAsJson(
HttpServletResponse response,
Object obj) throws IOException {
response.setContentType("application/json");
String res = _gson.toJson(obj);
PrintWriter out = response.getWriter();
out.print(res);
out.flush();
}
// Get models
// GET/JavaViewer/models/
// GET/JavaViewer/models/id
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String pathInfo = request.getPathInfo();
if(pathInfo == null || pathInfo.equals("/")){
Collection<Model> models = _modelsDb.values();
sendAsJson(response, models);
return;
}
String[] splits = pathInfo.split("/");
if(splits.length != 2) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String modelId = splits[1];
if(!_modelsDb.containsKey(modelId)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
sendAsJson(response, _modelsDb.get(modelId));
return;
}
// Adds new model in DB
// POST/JavaViewer/models
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String pathInfo = request.getPathInfo();
if(pathInfo == null || pathInfo.equals("/")){
StringBuilder buffer = new StringBuilder();
BufferedReader reader = request.getReader();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String payload = buffer.toString();
Model model = _gson.fromJson(payload, Model.class);
model.id = UUID.randomUUID().toString();
_modelsDb.put(model.id, model);
sendAsJson(response, model);
}
else {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
}
// Updates a model in DB
// PUT/JavaViewer/models/id
protected void doPut(
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String pathInfo = request.getPathInfo();
if(pathInfo == null || pathInfo.equals("/")){
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String[] splits = pathInfo.split("/");
if(splits.length != 2) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String modelId = splits[1];
if(!_modelsDb.containsKey(modelId)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
StringBuilder buffer = new StringBuilder();
BufferedReader reader = request.getReader();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String payload = buffer.toString();
Model model = _gson.fromJson(payload, Model.class);
model.id = modelId;
_modelsDb.put(modelId, model);
sendAsJson(response, model);
}
// Deletes a model in DB
// DELETE/JavaViewer/models/id
protected void doDelete(
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String pathInfo = request.getPathInfo();
if(pathInfo == null || pathInfo.equals("/")){
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String[] splits = pathInfo.split("/");
if(splits.length != 2) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String modelId = splits[1];
if(!_modelsDb.containsKey(modelId)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
Model model = _modelsDb.get(modelId);
_modelsDb.remove(modelId);
sendAsJson(response, model);
return;
}
}
@leonlee887
Copy link

Great work, thanks

@DaeAkin
Copy link

DaeAkin commented May 28, 2019

Thanks.

@danielscarvalho
Copy link

Simple and useful! Thanks!

@lee-gyu
Copy link

lee-gyu commented Aug 27, 2020

thanks for sharing this code

@evicoach
Copy link

Thanks for sharing

@MarsMan13
Copy link

Useful! Thanks for your sharing :)

@ftvdexcz
Copy link

thanks you for sharing this code

@gathrean
Copy link

thank you for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment