Java Reference
In-Depth Information
return "<html><body><h1>Hello from REST!</body></h1></html>";
}
}
Using REST is easy with Jersey. Here you create a POJO that is annotated with
javax.ws.rs.Path . Its value attribute indicates the path where this resource will be made
available. The resource path follows the adapter servlet path, so you will be able to reach
an instance of this class at <server>:<port>/<context>/<jersey-servlet-mapping>/
<path-value> . The Jersey servlet mapping is defined in the web.xmlfile.
You also need to annotate your method that does the work. By using the JAX-RS @GET an-
notation, you indicate that the sayHello method is the one you want to respond to HTTP GET
requests. Then use the @ProduceMime annotation to indicate the MIME type you want to re-
turn on response. Here you use text/html , but you can use any MIME type that makes sense
for your application, including text/xml and image/gif .
Example 8-7 is the web.xmlfile that causes Jersey to intercept requests that follow the re-
sources path. If you've used JavaServer Faces before, this intercepting servlet idea should be
familiar.
Example8-7.The web.xml file specifying the Jersey Adapter servlet
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>JerseyAdapter</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyAdapter</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Now you can deploy your WAR and open a browser at http://localhost:8080/HelloWorld/re-
sources/helloRest to see your message.
Search WWH ::




Custom Search