Warning
This page is located in archive.

Lesson 2 - REST, JAX-RS, Jersey

Lesson plan

  • Homework check
  • REST intro
  • JAX-RS application with Glassfish/Jersey example

REST

See the presentation describing REST.

Simple example

The example may be downloaded here: jersey-sample.zip

Check the sample, see how it works. The application is inspired by the sample 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:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>${version.servlet.api}</version>
    <scope>provided</scope>
</dependency>
                    
<!-- Jersey -->		
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>${version.jersey}</version>
</dependency>

<dependency>
     <groupId>org.glassfish.jersey.media</groupId>
     <artifactId>jersey-media-moxy</artifactId>
     <version>${version.jersey}</version>
</dependency>

Descriptor

To deploy the application the minimal web.xml descriptor must be used:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    id="WebApp_ID" version="3.1">    
  <session-config>
    <session-timeout>30</session-timeout>
    <cookie-config>
      <name>SESSIONID</name>
    </cookie-config>
  </session-config>
</web-app>

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<Message> getAllMessages() throws Exception{
        List<Message> 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 Jersey Guide
courses/a4m36aos/cviceni/class_8_10_2015.txt · Last modified: 2016/10/13 06:02 by kopriste