Warning
This page is located in archive.

REST API for JEE and GAE applications

In this exercise we will learn how to create resource accessible through REST service. We will create the same functionality as in the last exercise.

Jersey

Jersey is reference implementation for building RESTful Web services. To use Jersey in Google App Engine you need to download the 1.12 version of the library, unzip, copy content of the lib directory to war/WEB-INF/lib of your project and add the new .jar files in the war/WEB-INF/lib directory to your project using Project → Properties → Java Build Path → Add JARs… in Eclipse.

JSON

JSON stands for JavaScript Object Notation. We will use serialize Java objects to JSON on server side and transform JSON to JavaScript objects on the client side. JSON is supported with most of the modern browsers out of the box. If you want to be sure that your application will run also in the older browsers you can download this JavaScript library that will add JSON support if it is not already present.

Exercise 1

Create new project with sample code in package practice4.

Add Jersey jars to your project.

Register Jersey servlet in your web.xml file:

<servlet>
	<servlet-name>Rest</servlet-name>
	<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
	<init-param> 
	<param-name>com.sun.jersey.config.feature.DisableWADL</param-name> 
	<param-value>true</param-value> 
	</init-param>
	<init-param>
	<param-name>com.sun.jersey.config.property.packages</param-name>
	<param-value>practice4</param-value>
</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>Rest</servlet-name>
	<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Note that there is several parameters. The parameter com.sun.jersey.config.feature.DisableWADL is there for compatibility with Google App Engine and the parameter com.sun.jersey.config.property.packages specifies packages in which Jersey will search for classes representing RESTfull services (you can specify more packages separated with comma). The servlet will serve all request send to /rest/* url (e.g. http://localhost:8888/rest/greeting/) and redirect them to classes representing RESTfull services.

Create new class GreetingService in package practice4. This class will represent RESTfull service:

package practice4;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
 
@Path("/greeting/") /* Maps the service on url localhost:8888/rest/greeting/ */
public class GreetingService {
 
    @GET /* Response on the GET request */
    @Produces("text/plain") /* The result of response is plain text */
    public String helloWorld() {
	return "Hello world!!!";
    }   
}
Start the web server and enter http://localhost:8888/rest/greeting/ into browser.

Exercise 2

Download and open this project. Study the classes Message and MessageService and JSP guestbook.jsp.

Run the web server and create several messages.

Enter http://localhost:8888/rest/message/ into browser to see the response in JSON for list of messages.

Find out id of one of the messages (e.g. in link “Vymazat zprávu”) and enter http://localhost:8888/rest/message/<id> into browser to see the response in JSON for one message. You have to replace <id> in the url with the real id.

Add new functionality to the web application:

  • Provide functionality “Vymazat všechny zprávy” and “Vymazat zprávu” only to the admin.
  • Allow to modify text of message, but only to author of the message.
  • Allow to respond on the message (you can see how it is solved in the solved exercise from the previous practice).
courses/a4m39wa2/tutorials/04/start.txt · Last modified: 2014/12/03 13:36 (external edit)