将多个servlet添加到一个web.xml

时间:2016-05-10 06:27:38

标签: servlets jersey jax-rs servlet-mapping

我想将Jersey项目B(已经运行良好)合并/添加到一个新的Jersey项目A中,它将充当过滤器/安全层。因此,作为基本步骤,我在项目A的构建路径上向项目B添加了依赖项,并将其添加到构建路径中的部署程序集中。我从this帖子了解到,我可以通过将servlet放在同一个web.xml中并使用<servlet-mapping>对它们进行不同的映射来实现。当我试图访问项目B的资源时,我没有任何运气。

web.xml

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>org.abc.def.ba, org.pqr.xyz</param-value>
    </init-param>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>org.abc.def.ba.CustomApplication</param-value>
    </init-param> 
    <load-on-startup>1</load-on-startup>
</servlet>

 <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

<!-- The Servlet of Project B -->
<servlet>
    <servlet-name>Jersey Web Application 2</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-     class>

<init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value> org.pqr.xyz.MyApplication</param-value>
    </init-param> 

    <!-- Register resources and providers under my.package. -->
   <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value> org.pqr.xyz</param-value>
    </init-param>

    <!-- Register my custom provider (not needed if it's in my.package) AND LoggingFilter. -->
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value> org.pqr.xyz.mapper.ObjectMapperProvider</param-value>
    </init-param>

     <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value> org.pqr.xyzr</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
        <param-value>com.porterhead.rest.filter.ResourceFilterFactory</param-value>
    </init-param>
    <init-param>
        <param-name>readOnly</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application 2</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

因此,当我尝试访问http://localhost:8080/ba/webapi/myresource时,它运行良好。但是当我尝试http://localhost:8080/ba/rest/newresource时,我收到404错误。我知道我错过了什么。感谢您对此的指导。

1 个答案:

答案 0 :(得分:0)

如果您使用的是servlet 3.0,则可以使用@ApplicationPath批注为不同的资源使用不同的应用程序路径。如果您不想使用注释,可以使用下面的web.xml实现相同的

<web-app>
<servlet>
    <servlet-name>org.foo.rest.MyApplication</servlet-name>
</servlet>
...
<servlet-mapping>
    <servlet-name>org.foo.rest.MyApplication</servlet-name>
    <url-pattern>/resources</url-pattern>
</servlet-mapping>
...

有关详细信息,请参阅泽西岛documentation了解部署选项。