Topic
  • No replies
gallardo
gallardo
2 Posts

Pinned topic Feature Focus Week: JAX-RS Updates

‏2010-09-17T12:29:51Z |
For the WAS 8.0 Beta, we've updated the the JAX-RS support that was originally provided in the Alpha release to include support for Servlet 3.0. For most that update will be transparent. But, by taking advantage of some of the mechanisms that Servlet 3.0 provides to web applications, we now support new patterns for packaging your JAX-RS classes within a web application.

In Servlet 2.5 environments and prior, a web.xml (like the following) is required to register the JAX-RS servlet with the web container.


<servlet> <servlet-name>IBM JAX-RS Servlet</servlet-name> <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.ibm.jaxrs.example.MyApp</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>


You can see that the above declaration is much like any servlet declaration.

Pattern 1: Removing the web.xml


If you find that you are only including the web.xml for the purposes of registering the JAX-RS servlet, you now have the option of removing that file altogether. By including an javax.ws.rs.core.ApplicationPath annotation on your application sub-class, the JAX-RS runtime can find the other information it needs to register your resources and providers. Here's a sample Application sub-class configured with the @ApplicationPath annotation.


@ApplicationPath(
"/resources") 

public 

class MyApplication 

extends Application 
{ @Override 

public Set<Class<?>> getClasses() 
{ ... 
} 
}


With this in place, all classes annotated with @Path will be available under the "/resources" URI. Consider the following simple JAX-RS resource.


@Path(
"/photos") 

public 

class PhotoCollection 
{ @GET @Produces(
"application/json") 

public Response getPhotos() 
{ ... 
}   
}


If the above resource existed in an application with annotated application sub-class, the resource would become available at the URL:

http://{host}:{post}/{context root}/resources/photos

Pattern 2: Declaring multiple Application sub-classes


Another new option is to declare multiple Application sub-classes in your web.xml. Let's say, for example, that you have two different collections of resources in your application. One collection of resources could be for basic access and presentation of data that is available on the server. The other collection could be a set of administrative APIs for the same application. You could register the two sets of resources independently with two different application sub-class references in the web.xml.


<?xml version=
"1.0" encoding=
"UTF-8"?> <web-app xmlns=
"http://java.sun.com/xml/ns/j2ee" xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=
"http://java.sun.com/xml/ns/j2ee http:
//java.sun.com/xml/ns/j2ee/web-app_3_0.xsd" version=
"3.0">   <!-- Application Resource Registration -->   <servlet> <servlet-name>com.ibm.jaxrs.example.MyApplication</servlet-name> </servlet> <servlet-mapping> <servlet-name>com.ibm.jaxrs.example.MyApplication</servlet-name> <url-pattern>/resources
/*</url-pattern> </servlet-mapping> <!-- Admin Resource Registration --> <servlet> <servlet-name>com.ibm.jaxrs.example.AdminApplication</servlet-name> </servlet> <servlet-mapping> <servlet-name>com.ibm.jaxrs.example.AdminApplication</servlet-name> <url-pattern>/admin/resources/*</url-pattern> </servlet-mapping> </web-app>


The pattern above allows you to separate administration and configuration for the two distinct roles involved. Because the admin portion of the application is in its own servlet, you can include additional security configuration info for that servlet that would not apply to the base resources.

If you questions about the new deployment options or would like to know more about JAX-RS in general, please post your responses here or in a new thread. We would love to hear from you and appreciate your feedback.

-Nick Gallardo (technical lead, JAX-RS)