jersey spring jackson

来源:互联网 发布:js json数组排序 编辑:程序博客网 时间:2024/05/07 07:01

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文件

 

Web.xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.   
  7.     <context-param>  
  8.         <param-name>contextConfigLocation</param-name>  
  9.         <param-value>classpath:applicationContext.xml</param-value>  
  10.     </context-param>  
  11.   
  12.     <listener>  
  13.         <listener-class>  
  14.             org.springframework.web.context.ContextLoaderListener  
  15.         </listener-class>  
  16.     </listener>  
  17.   
  18.     <listener>  
  19.         <listener-class>  
  20.             org.springframework.web.context.request.RequestContextListener  
  21.         </listener-class>  
  22.     </listener>  
  23.   
  24.   
  25.     <!-- Jersey-->  
  26.     <servlet>  
  27.         <servlet-name>JerseyServlet</servlet-name>  
  28.         <servlet-class>  
  29.             com.sun.jersey.spi.spring.container.servlet.SpringServlet  
  30.         </servlet-class>  
  31.         <!-- 系统启动时扫描的包的路径-->  
  32.         <!--   
  33.             <init-param>  
  34.             <param-name>  
  35.             com.sun.jersey.config.property.packages  
  36.             </param-name>  
  37.             <param-value>test.jersey_spring.demo.resource</param-value>  
  38.             </init-param>  
  39.         -->  
  40.         <!-- 使用jackson处理json -->  
  41.         <init-param>  
  42.             <param-name>  
  43.                 com.sun.jersey.api.json.POJOMappingFeature  
  44.             </param-name>  
  45.             <param-value>true</param-value>  
  46.         </init-param>  
  47.         <load-on-startup>1</load-on-startup>  
  48.     </servlet>  
  49.   
  50.     <servlet-mapping>  
  51.         <servlet-name>JerseyServlet</servlet-name>  
  52.         <url-pattern>/rest/*</url-pattern>  
  53.     </servlet-mapping>  
  54.   
  55. </web-app>  
 

  4. 配置applicationContext.xml文件

applicationContext.xml

Xml代码  收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  6.     http://www.springframework.org/schema/context  
  7.     http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  8.       
  9.     <context:component-scan base-package="test.jersey_spring.demo" />  
  10.       
  11. </beans>  

 5. 现在可以写类测试了,应当会报错,当使用jackson时

Error

Java代码  收藏代码
  1. 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 的版本就可以了

原创粉丝点击