@POST
- To test your POST requests, you need a REST client. I use POSTMAN chrome browser extension, you can use anything else you will find on the web
- HTTP 201 - created
- Note how we do not have the method-specific @Path, the parent @Path is used in this case, so our request endpoint will be http://localhost:8080/unh/rest/sandbox
- We are also now Consuming data and to make sure whoever makes the requests against our URL adheres to the protocols we are comfortable with, we specify the data we can understand using @Consumes annotation
package test; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.fasterxml.jackson.databind.ObjectMapper; @Path("post") public class POSTSandbox { ObjectMapper mapper = new ObjectMapper(); @POST @Produces(MediaType.APPLICATION_JSON) @Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN }) public Response createBook(String bookStr) { try { Book book = mapper.readValue(bookStr, Book.class); String bookJSON = mapper.writeValueAsString(book); return Response.status(201).entity(bookJSON).build(); } catch (Exception e) { return Response.status(500).entity(e.toString()).build(); } } } class Book { private String title; private String author; private String ISBN; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getISBN() { return ISBN; } public void setISBN(String iSBN) { ISBN = iSBN; } }
Invoking your POST Service from POSTMAN
{"title":"ABC", "author":"Gula", "isbn":"1234"}
{"title":"ABC", "author":"Gula", "isbn":"1234"}
@PUT
package test; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.fasterxml.jackson.databind.ObjectMapper; @Path("put") public class POSTSandbox { ObjectMapper mapper = new ObjectMapper(); @PUT @Path("{bookid}") @Produces(MediaType.APPLICATION_JSON) @Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN }) public Response createBook(String bookStr, @PathParam("bookid") String bookid) { try { return Response.status(200).entity(bookid).build(); } catch (Exception e) { return Response.status(500).entity(e.toString()).build(); } } }
@DELETE
package test; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.fasterxml.jackson.databind.ObjectMapper; @Path("delete") public class POSTSandbox { ObjectMapper mapper = new ObjectMapper(); @DELETE @Path("{bookid}") @Produces(MediaType.APPLICATION_JSON) @Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN }) public Response createBook(String bookStr, @PathParam("bookid") String bookid) { try { return Response.status(200).entity(bookid).build(); } catch (Exception e) { return Response.status(500).entity(e.toString()).build(); } } }