我想将一个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
错误。我知道我错过了什么。我感谢你在这方面的指导。
如果您使用的是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>
...
有关详细信息,请参阅Jersey文档了解部署选项。