REST with jersy cheetsheet
REST with jersy cheetsheet
-------------------------
hello world
----------------
Step 1: create dynamic web appication and copy jar into lib and add to build path
<servlet>
<servlet-name>jersy</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-
class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-
name>
<param-value>com.bookapp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
@Path("/hello")
public class HelloResouce {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
}
-----------------------------------------
Ex 2: @QueryParam and @PathParam
@GET
@Path("/{uname}")
@Produces(MediaType.TEXT_PLAIN)
public String hello2(@PathParam("uname")String name) {
return "hello 2"+" name: "+name ;
}
//http://localhost:8090/app2/api/hello?uname=raj&city=noida
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello2(@QueryParam("uname")String name,
@QueryParam("city")String city) {
return "hello 2"+" name: "+name +" "+ city;
}
--------------------------------------------------
Ex 3: CRUD application Bookapp
---------------------------------------------------
Step 1: create dynamic web appication and copy jar into lib and add to build path
<servlet>
<servlet-name>jersy</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-
class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-
name>
<param-value>com.bookapp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
@Override
public List<Book> getAll() {
// if(1==1)
// throw new RuntimeException("it is some db error");
@Override
public Book getBookById(int bookId) {
Book book= books.get(bookId);
if(book==null)
throw new BookNotFoundException("book with id "+ bookId +" is not
found");
return book;
}
@Override
public Book addBook(Book book) {
book.setId(books.size()+1);
books.put(book.getId(), book);
return book;
}
@Override
public Book updateBook(int bookId, Book book) {
Book bookToUpdate= getBookById(bookId);
bookToUpdate.setPrice(book.getPrice());
books.put(bookId, bookToUpdate);
return bookToUpdate;
}
@Override
public Book removeBook(int bookId) {
Book bookToRemove=getBookById(bookId);
books.remove(bookId);
return bookToRemove;
}
@Path("/api")
public class BookController {
public BookController() {
bookService = new BookServiceImpl();
}
// --------add an book---------
@POST
@Path("/books")
@Consumes("application/json")
@Produces("application/json")
public Book addBook(Book book) {
Book bookAdded= bookService.addBook(book);
return bookAdded;
}
// --------update an book---------
@PUT
@Path("/books/{id}")
@Consumes("application/json")
@Produces("application/json")
public Book updateBook(@PathParam("id") int id, Book book) {
return bookService.updateBook(id, book);
}
//--------------delete an book----------204
@DELETE
@Path("/books/{id}")
@Produces("application/json")
public Book deletById(@PathParam("id") int id) {
return bookService.removeBook(id);
}
}
--------------------------------------------------
Ex 3: CRUD application Bookapp: using correct http status code
---------------------------------------------------
@POST
@Path("/books")
@Consumes("application/json")
@Produces("application/json")
public Response addBook(Book book) {
Book bookAdded= bookService.addBook(book);
return Response.status(Status.CREATED).entity(bookAdded).build();
}
@DELETE
@Path("/books/{id}")
@Produces("application/json")
public Response deletById(@PathParam("id") int id) {
bookService.removeBook(id);
return Response.status(Status.NO_CONTENT).build();
}
--------------------------------------------------
Ex 3: CRUD application Bookapp: using Exception handling
---------------------------------------------------
Step 1: create exception wrapper (that hold the exception information)
@Provider
public class BookNotFoundExHandler implements
ExceptionMapper<BookNotFoundException>{
@Override
public Response toResponse(BookNotFoundException ex) {
ErrorMessage errorMessage=new ErrorMessage();
errorMessage.setMessage(ex.getMessage());
errorMessage.setStatusCode(404);
errorMessage.setToContect("[email protected]");
return Response.status(Status.NOT_FOUND).entity(errorMessage).build();
}
Agenda:
* Jersy crud application
* Understanding why REST is called REST
* Returning correct status code
* Exception handing
* Hateoas
http://localhost:8090/app2/api/hello/kapil/delhi
@Service
public class BookDaoMapImp implements BookDao {
@Override
public List<Book> getAllBooks() {
return new ArrayList<Book>(books.values());
}
@Override
public Book getBookById(int bookId) {
return books.get(bookId);
}
@Override
public Book addBook(Book book) {
book.setId(books.size() + 1);
books.put(book.getId(), book);
return book;
}
@Override
public Book updateBook(Book book) {
if (book.getId() <= 0)
return null;
else
books.put(book.getId(), book);
return book;
}
@Override
public Book removeBook(int bookId) {
return books.remove(bookId);
}