Configuring the web.xml file for JAX-RS filters
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 define filters that indicate the possible URLs on which the filter can be invoked.
About this task
You 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 filter 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 defined in the web.xml is appended to the base URL. Filters do not append a path to the resource base URL. Instead, filter URL mappings indicate the possible URLs on which the filter can be invoked. For instance, if a root resource has a @javax.ws.rs.Path value of myresource, the final URL of the resource is http://<your_hostname>:<your Web_container_port>/<context_root_of_Web_application>/myresource. The URL mapping pattern for the filter must match myresource for the root resource to be served correctly. For this example, you can use /* or /myresource for the URL pattern. When there are multiple resources in the application, the URL pattern for the filter must match all of the resources. The /* pattern is a common value for the filter.
If the incoming request URL does not match any JAX-RS resources in the JAX-RS application, the request is passed to the rest of the filter chain. Depending on the application, you might want to use the filter behavior so that requests are served by the JAX-RS application, or if there is no JAX-RS resource available, the request can proceed to an underlying web container artifact, such as a JavaServer Pages (JSP). If the web container has no matching artifact, then the web container is responsible for the error response.
Procedure
Results
Example
The following example illustrates a WEB-INF/web.xml file that is configured to apply filters to a JAX-RS application. This example defines the RestApplication1 filter. If an incoming request URL matches a resource in the RestApplication1 filter, the response is generated from RestApplication1 filter. If the incoming request URL does not match a resource in the RestApplication1 filter but matches a resource in OtherRestApplicationFilter, then the response is generated from the OtherRestApplicationFilter filter. If the incoming URL does not match either filter, then the request can be served from another web container artifact, such as a JSP.
<?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">
<filter>
<filter-name>RestApplication1</filter-name>
<filter-class>com.ibm.websphere.jaxrs.server.IBMRestFilter</filter-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>
</filter>
<filter>
<filter-name>OtherRestApplicationServlet</filter-name>
<filter-class>com.ibm.websphere.jaxrs.server.IBMRestFilter</filter-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>
</filter>
<filter-mapping>
<filter-name>RestApplication1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>OtherRestApplicationServlet</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
What to do next
Assemble the web application.