2017-01-09 71 views
0

我試圖讓球衣+彈簧整合。Jersey + Spring集成和web.xml

我有關於web.xml配置的問題。

這個例子包括SpringServlet:

<servlet> 
    <servlet-name>jersey-serlvet</servlet-name> 
    <servlet-class> 
     com.sun.jersey.spi.spring.container.servlet.SpringServlet 
    </servlet-class> 
    <init-param> 
     <param-name> 
          com.sun.jersey.config.property.packages 
        </param-name> 
     <param-value>com.mkyong.rest</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

https://www.mkyong.com/webservices/jax-rs/jersey-spring-integration-example/

但另一個例子並不包括SpringServlet。

https://github.com/jersey/jersey/blob/2.25/examples/helloworld-spring-webapp/src/main/webapp/WEB-INF/web.xml

最後一個例子包括:

<init-param> 
     <param-name>javax.ws.rs.Application</param-name> 
     <param-value>org.glassfish.jersey.examples.helloworld.spring.MyApplication</param-value> 
    </init-param> 

和在所有MyApplication類它延伸ResourceConfig和寄存器RequestContextFilter.class。

我的問題是。

1-)兩個web.xml配置的主要區別是什麼? 2)爲什麼第二個例子擴展ResourceConfig並註冊RequestContextFilter.class?

回答

0

球衣ServletContainer(SpringServlet在這種情況下)可以以多種方式進行初始化..

第一個例子只是註冊包來尋找所需的資源,因爲它是一個簡單的例子。

但是在需要定義多個配置的情況下,可以使用名稱爲ServletProperties.JAXRS_APPLICATION_CLASS的init參數來完成,該參數指的是實現Application的類。

此應用程序實例(org.glassfish.jersey.examples.helloworld.spring.MyApplication)可以定義像映射器,過濾器等所有servlet級別配置。