====== Lesson 2 - REST, JAX-RS, Jersey======
===== Lesson plan =====
* Homework check
* REST intro
* JAX-RS application with Glassfish/Jersey example
===== REST =====
See the presentation describing[[http://old.kwarc.info/projects/students/papers/Rest.ppt | REST]].
===== Simple example =====
The example may be downloaded here: {{:courses:a4m36aos:cviceni:jersey-sample.zip|}}
Check the sample, see how it works. The application is inspired by the sample [[http://https://www.nabisoft.com/tutorials/java-ee/producing-and-consuming-json-or-xml-in-java-rest-services-with-jersey-and-moxy|REST]] application. The application provides two resources - a message resource and a person resource. The application supports just GET method for both resources, therefore you are able to get person and message resources.
==== Jersey maven ====
The following are the dependencies we are using:
javax.servlet
javax.servlet-api
${version.servlet.api}
provided
org.glassfish.jersey.containers
jersey-container-servlet
${version.jersey}
org.glassfish.jersey.media
jersey-media-moxy
${version.jersey}
==== Descriptor ====
To deploy the application the minimal web.xml descriptor must be used:
30
SESSIONID
==== Message resource ====
@Path("/message")
public class MessageResource {
@GET
@Path("ping")
public String getServerTime() {
return "received ping on "+new Date().toString();
}
@GET
@Produces({MediaType.APPLICATION_JSON}) //add MediaType.APPLICATION_XML if you want XML as well (don't forget @XmlRootElement)
public List getAllMessages() throws Exception{
List messages = new ArrayList<>();
Message firstMessage = new Message();
firstMessage.setDate(new Date());
firstMessage.setFirstName("Nabi");
firstMessage.setAge(30);
firstMessage.setText("Hello World!");
messages.add(firstMessage);
Message secondMessage = new Message();
secondMessage.setDate(new Date());
secondMessage.setFirstName("Francis");
secondMessage.setAge(31);
secondMessage.setText("It's in the game.");
messages.add(secondMessage);
return messages; //do not use Response object because this causes issues when generating XML automatically
}
@GET
@Produces({MediaType.APPLICATION_JSON}) //add MediaType.APPLICATION_XML if you want XML as well (don't forget @XmlRootElement)
@Path("text/{textParam}")
public Message getMessage(@PathParam("textParam") String textParam) throws Exception{
Message message = new Message();
message.setDate(new Date());
message.setFirstName("Vladimir");
message.setAge(35);
message.setText(textParam);
return message;
}
}
==== Client for testing ====
For testing, you can use one of the following clients:
* Simple REST client - Chrome
* REST client - Firefox
Deploy the application to web server and go to the address http://localhost:8080/rest/:
==== Tasks ====
* Modify the resources such that they return not only JSON, but also XML (remember to call them with correct headers: Content-type: application/xml}
* Find out how to implement PUT, POST and DELETE methods
* Find out how to implement Query parameters - use [[https://jersey.java.net/documentation/latest/|Jersey Guide]]