我想将 Jersey 项目 B(已经运行良好)合并/添加到新的 Jersey 项目 A 中,该项目将充当过滤器/安全层。因此,作为一个基本步骤,我在项目 A 的构建路径上添加了对项目 B 的依赖,并将其添加到构建路径中的部署程序集。我从这篇文章中了解到,我可以通过将 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
错误。我知道我错过了一些东西。感谢您对此的指导。