+

Search Tips   |   Advanced Search

Configure web.xml for JAX-RS servlets

The web.xml file contains information about the structure and dependencies of web components in the module and describes how the components are used at run time. To enable the web container to run Java API for RESTful Web Services (JAX-RS) applications, we can configure web.xml to point directly to the IBM JAX-RS servlet. When using servlets, we can define a servlet path in web.xml appended to the base URL.

We can configure web.xml for the web application to enable the JAX-RS application code. We can specify an IBM specific JAX-RS servlet to use to run the JAX-RS code. The web.xml file provides configuration and deployment information for the web components that comprise a web application.

When using servlets, any servlet path defined in the web.xml is appended to the base URL. For example, if a root resource has a @javax.ws.rs.Path value of myresource and a servlet path of myservletpath, the final URL of the resource is...

  1. Edit WEB-INF/web.xml.

  2. Add the following servlet definition to the WEB-INF/web.xml file. In the following servlet, you must replace the unique_servlet_name with your unique servlet name. Also, replace the Java_class_name variable with the full Java package and class name of the javax.ws.rs.core.Application subclass.
    <servlet>
         <servlet-name>unique_servlet_name</servlet-name>
         <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
         <init-param>
             <param-name>javax.ws.rs.Application</param-name>
             <param-value>Java_class_name </param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
    </servlet>

  3. Optional. If there are multiple JAX-RS application subclasses needed in the same web application, include an additional servlet initialization parameter, requestProcessorAttribute, in the servlet definition that we add to the WEB-INF/web.xml file. In the following servlet, you must replace the unique_servlet_name with your unique servlet name, the Java_class_name variable with the full Java package and class name of the javax.ws.rs.core.Application subclass, and the unique_identifier variable with a unique identifier.
    <servlet>
         <servlet-name>unique_servlet_name_a</servlet-name>
         <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
         <init-param>
             <param-name>javax.ws.rs.Application</param-name>
             <param-value>Java_class_name_a </param-value>
         </init-param>
         <init-param>
             <param-name>requestProcessorAttribute</param-name>
             <param-value>unique_identifier_a</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
         <servlet-name>unique_servlet_name_b</servlet-name>
         <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
         <init-param>
             <param-name>javax.ws.rs.Application</param-name>
             <param-value>Java_class_name_b</param-value>
         </init-param>
         <init-param>
             <param-name>requestProcessorAttribute</param-name>
             <param-value>unique_identifier_b </param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
    </servlet>

  4. Add servlet mappings in the WEB-INF/web.xml file for each servlet definition. The servlet path is appended to the context root of the web application.
    <servlet-mapping>
         <servlet-name>servlet_name</servlet-name>
         <url-pattern>servlet_pattern_path</url-pattern>
    </servlet-mapping>
    For example, if the servlet_pattern_path is /restapi/*, all valid requests start at the following URL:

      http://host:port/ContextRoot/restapi/


Results

After editing the WEB-INF/web.xml file, the web application is configured for the JAX-RS application.


Example

The following example illustrates a WEB-INF/web.xml file that configures a servlet path for a JAX-RS application. The servlet path defined in web.xml is appended to the base URL.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"  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_2_4.xsd">
 
<servlet>
     <servlet-name>RestApplication1</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.rest.sample.app1.MyApplication</param-value>
     </init-param>
     <init-param>
         <param-name>requestProcessorAttribute</param-name>
         <param-value>restApplication1ProcessorID</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
</servlet>
 <servlet>
     <servlet-name>OtherRestApplicationServlet</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.rest.other.sample.OtherApplication </param-value>
     </init-param>
     <init-param>          <param-name>requestProcessorAttribute</param-name>
         <param-value>otherRestApplicationID </param-value>
     </init-param>      <load-on-startup>1</load-on-startup>
</servlet>
 <servlet-mapping>
     <servlet-name> RestApplication1</servlet-name>
     <url-pattern>/rest/api/*</url-pattern>
</servlet-mapping>
 <servlet-mapping>
     <servlet-name>OtherRestApplicationServlet /servlet-name>
     <url-pattern>/other/*</url-pattern>
</servlet-mapping>
</web-app>


What to do next

Assemble the web application.


Related tasks

  • Implement JAX-RS web applications
  • Configure web.xml for JAX-RS filters