Network Deployment (Distributed operating systems), v8.0 > Develop and deploying applications > Develop web services - RESTful services > Develop JAX-RS web applications > Configure JAX-RS web applications


Configure the web.xml file for JAX-RS servlets

The web.xml file contains information about the structure and external 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, you can configure the web.xml file to point directly to the IBM JAX-RS servlet. When using servlets, you can define a servlet path in the web.xml file that is appended to the base URL.

We can configure the web.xml file for your web application to enable the JAX-RS application code. You can specify an IBM specific JAX-RS servlet to use to run your JAX-RS code. The web.xml file provides configuration and deployment information for the web components that comprise a web application. Read about configuring the web.xml file for JAX-RS to learn more about this deployment descriptor file.

When using servlets, any servlet path that is 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 http:// <your_hostname>: <your Web_container_port>/ <context_root_of_Web_application>//myservletpath/myresource.


Procedure

  1. Open the WEB-INF/web.xml file.

  2. Add the following servlet definition to the WEB-INF/web.xml file. In the following servlet, 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 you add to the WEB-INF/web.xml file. In the following servlet, 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://
    <your_hostname>:
    <your Web_container_port>/
    <context_root_of_Web_application>/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 that is defined in the web.xml file 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.
Implement JAX-RS web applications
Configure the web.xml file for JAX-RS filters

+

Search Tips   |   Advanced Search