1. 下载 jersey 和 spring
http://jersey.java.net/
http://www.springsource.org/download/community?project=Spring%2520Framework
http://wiki.fasterxml.com/JacksonDownload
jersey-archive-1.12.zip
spring-framework-3.1.1.RELEASE.zip
jackson 1.9.5
2. eclipse新建web项目导入jar包
jersey-archive-1.12\lib\*.jar
spring-framework-3.1.1.RELEASE\dist
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
3. 配置web.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
-
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
-
- <listener>
- <listener-class>
- org.springframework.web.context.request.RequestContextListener
- </listener-class>
- </listener>
-
-
- <!-- Jersey-->
- <servlet>
- <servlet-name>JerseyServlet</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>test.jersey_spring.demo.resource</param-value>
- </init-param>
- -->
- <!-- 使用jackson处理json -->
- <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>JerseyServlet</servlet-name>
- <url-pattern>/rest/*</url-pattern>
- </servlet-mapping>
-
- </web-app>
4. 配置applicationContext.xml文件
applicationContext.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
-
- <context:component-scan base-package="test.jersey_spring.demo" />
-
- </beans>
5. 现在可以写类测试了,应当会报错,当使用jackson时
Error
- java.lang.VerifyError: (class: org/codehaus/jackson/map/ObjectMapper, method: writeValueAsBytes signature: (Ljava/lang/Object;)[B) Incompatible argument to function
jersey-1.12 里使用的jackson是1.9.1版本的,替换为1.9.5-asl 的版本就可以了