jersey-spring,jersey集成spring

jersey集成spring相對來講比較簡單,須要注意的幾點是:git

一、引入maven依賴jersey-spring,其中包含了spring的相關依賴,若是和項目中所使用的spring版本相沖突,能夠經過exclusions清除掉。web

二、確保jersey資源類所在的包路徑,能被spring掃描到,即spring配置中的<context:component-scan base-package="" />。spring

三、確保jersey資源類打上註解@Component,這樣能夠注入spring容器的對象實例。app

四、在web.xml中配置com.sun.jersey.spi.spring.container.servlet.SpringServlet。maven

五、如何測試,請看JerseyTest
測試

項目源碼參考
ui

jersey-spring的maven依賴,去除了spring相關依賴。具體配置,可進入上面的"項目源碼參考"。url

<!-- jersey-spring: 包含了jersey-servlet/jersey-server/jersey-core等,同時還包含了spring相關依賴。 -->
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-spring</artifactId>
    <version>1.19</version>
    <exclusions>
        <exclusion>
            <artifactId>spring-core</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-beans</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-context</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-web</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-aop</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
    </exclusions>
</dependency>

web.xml.net

<servlet>
    <servlet-name>jerseySpring</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jerseySpring</servlet-name>
    <url-pattern>/jersey/*</url-pattern>
</servlet-mapping>
相關文章
相關標籤/搜索