为 JAX-RS Servlet 配置 web.xml 文件
web.xml 文件包含有关模块中的 Web 组件的结构和外部依赖项的信息,并描述运行时如何使用组件。要使 Web 容器能够运行 Java™ API for RESTful Web Services (JAX-RS) 应用程序,您可以配置 web.xml 文件以直接指向 IBM® JAX-RS Servlet。使用 Servlet 时,您可以在 web.xml 文件中定义要对基本 URL 追加的 Servlet 路径。
关于此任务
您必须配置 Web 应用程序的 web.xml 文件才能启用 JAX-RS 应用程序代码。您可以指定特定于 IBM 的 JAX-RS Servlet 以用于运行 JAX-RS 代码。web.xml 文件提供有关包含 Web 应用程序的 Web 组件的配置和部署信息。请参阅有关为 JAX-RS 配置 web.xml 文件的信息,以了解有关此部署描述符文件的更多资料。
使用 Servlet 时,将对基本 URL 追加 web.xml 中定义的任何 Servlet 路径。例如,如果根资源的 @javax.ws.rs.Path 值为 myresource 并且 Servlet 路径为 myservletpath,那么该资源的最终 URL 将是 http://<your_hostname>:<your Web_container_port>/<context_root_of_Web_application>//myservletpath/myresource。
过程
结果
示例
以下示例说明用于为 JAX-RS 应用程序配置 Servlet 路径的 WEB-INF/web.xml 文件。web.xml 文件中定义的 Servlet 路径将追加到基本 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>
下一步做什么
组装 Web 应用程序。