1
我想將澤西島項目B(已經運行良好)合併/添加到新澤西項目A中,該項目將充當過濾器/安全層。因此,作爲一個基本步驟,我在項目A的構建路徑中向項目B添加了依賴項,並將其添加到構建路徑中的部署程序集。我從this的帖子瞭解到,我可以通過使servlet在相同的web.xml
中並使用<servlet-mapping>
以不同方式映射它們來實現。我沒有任何運氣,當我試圖訪問B項目的資源將多個servlet添加到一個web.xml中
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
錯誤。我知道我錯過了一些東西。我感謝你的指導。
我在做同樣的事情,請參考我的代碼。另外,當我試圖構建並運行Tomcat服務器時,我得到了'java.lang.ClassNotFoundException:org.pqr.xyzr.MyApplication'。你有沒有想到這個的原因? – vardhinisuresh27
在你的web.xml中,第二個項目被評論。你能上傳最新版本的web.xml嗎? – ron
它沒有被評論,它是因爲使用了「/ *」,它是對下面的代碼進行註釋。發佈的代碼是當前的web.xml。謝謝! – vardhinisuresh27