Using Spring and Jersey together

。_饼干妹妹 提交于 2021-01-29 18:42:17

问题


I want to create a small standalone RESTful API web-app which I can deploy. I want to use Spring for dependency injection and Jersey for the RESTful stuff.

Is this something that can be done? Or should I be using whatever RESTful features that Spring uses?

If using both Spring and Jersey together makes sense, how do I combine them? What does my web.xml need to have in it?


回答1:


integration is quite easy. you can find a lot tutorial on net. see example here




回答2:


Well I just worked on the same technology and here is something which I used in my application.

This dependency are required for Spring+Jersey in your pom.xml

 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>3.1.1.RELEASE</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-beans</artifactId>
     <version>3.1.1.RELEASE</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-hibernate3</artifactId>
     <version>2.0.8</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>3.1.1.RELEASE</version>
 </dependency>

 <dependency>
     <groupId>com.sun.jersey</groupId>
     <artifactId>jersey-json</artifactId>
     <version>1.12</version>         
 </dependency>
 <dependency>
     <groupId>com.sun.jersey</groupId>
     <artifactId>jersey-core</artifactId>
     <version>1.12</version>         
 </dependency>
 <dependency>
     <groupId>com.sun.jersey</groupId>
     <artifactId>jersey-server</artifactId>
     <version>1.12</version>

 </dependency>
 <dependency>
     <groupId>com.sun.jersey</groupId>
     <artifactId>jersey-client</artifactId>
     <version>1.12</version>

 </dependency>
 <dependency>
     <groupId>com.sun.jersey.contribs</groupId>
     <artifactId>jersey-spring</artifactId>
     <version>1.12</version>
 </dependency>
 <dependency>
     <groupId>javax.ws.rs</groupId>
     <artifactId>jsr311-api</artifactId>
     <version>1.1.1</version>
 </dependency>



回答3:


your web.xml should contain jersey spring servlet

<servlet>
        <servlet-name>Jersey REST Service</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.vxl.appanalytix.service.impl</param-value>
        </init-param>
       <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

check these examples for more details rest with jersey struts2 spring3 not working jersey rest services showing exception javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException



来源:https://stackoverflow.com/questions/20351422/using-spring-and-jersey-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!