Java Reference
In-Depth Information
Creating Hello World with Jersey
Problem
You want to create the simplest REST application with the Jersey implementation of JAX-RS.
Solution
Put the Jersey JARs on your project's classpath, and then create a POJO for your resource an-
notated with @Path to indicate the path at which your resource will be available. Then add a
method annotated with one of the HTTP method annotations such as @GET to indicate that it
should be invoked when the resource receives a request using that HTTP method. Indicate the
MIME type that the method will respond with. Make sure that you've updated your web.xml
file to cause the Jersey adapter servlet to be invoked against your resource path.
Discussion
Getting started with Jersey could hardly be easier. Creating a Hello World class is simple and
straightforward, as shown in Example 8-6 .
Example8-6.A RESTful Hello World application using Jersey
package com.soacookbook;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.ProduceMime;
/**
* Simplest REST web service.
*/
@Path("/helloRest")
public class HelloRest {
/**
* Retrieves representation of an instance of
* com.soacookbook.HelloRest.java
* @return a string with HTML text.
*/
@GET
@ProduceMime("text/html")
public String sayHello() {
Search WWH ::




Custom Search