使用springMVC注解@ResponseBody与jackson工具类在ajax请求中实现对象与json之间的相互转化

1.springmvc默认使用jackson来实现对象与json之间的相互转换的。在转换之前我需要引入两个springMVC依赖的两个jar包:
 jackson-core-asl(jackson核心包)和jackson-mapper-asl(json与对象转换工具包)
   获取jar包的方式:

  maven项目中的配置坐标如下:

 <dependency>  
    <groupId>org.codehaus.jackson</groupId>  
    <artifactId>jackson-mapper-asl</artifactId>  
    <version>1.9.13</version>  
</dependency>  
  
<dependency>  
    <groupId>org.codehaus.jackson</groupId>  
    <artifactId>jackson-core-asl</artifactId>  
    <version>1.9.13</version>  
</dependency>  
web工程的方式:直接在网上搜索相应的jar包下载下来,放入工程的lib目录下即可。
 2.开启springMVC的注解功能
   在springMVC项目中的*-dispacter.xml文件中配置<mvc:annotation-driven/>(表示开启springMVC的注解功能)
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:task="http://www.springframework.org/schema/task"  
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.0.xsd   
        http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
        http://www.springframework.org/schema/task  
        http://www.springframework.org/schema/task/spring-task-4.0.xsd  
        http://code.alibabatech.com/schema/dubbo          
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  
    <!-- 开启sprigMVC注解 -->  
    <mvc:annotation-driven/>  
    <!-- 其他的配置项-->  
     ................  
   </beans>  
3.实现代码
    定义一个对象:
public class Agent {  
            private String   agentName;  
            private Integer   agentId;  
            private String   address;  
            public String getAgentName() {  
                return agentName;  
            }  
            public void setAgentName(String agentName) {  
                this.agentName = agentName;  
            }  
            public Integer getAgentId() {  
                return agentId;  
            }  
            public void setAgentId(Integer agentId) {  
                this.agentId = agentId;  
            }  
            public String getAddress() {  
                return address;  
            }  
            public void setAddress(String address) {  
                this.address = address;  
            }  
  
        }  
  例如:在ajax请求springMVC的Controller方法后需要返回一个json格式的用户列表,则Conroller中的方法可以写成如下的方式:
/** 
    * 获取所有用户列表 
    */  
   @RequestMapping(value="getUserList",method=RequestMethod.GET)  
   public @ResponseBody List<Agent> getUserList(HttpServletRequest request, HttpServletResponse response){  
       //模拟数据创建一个数据对象1  
       Agent agent1 = new Agent();  
       agent1.setAgentName("张三");  
       agent1.setAgentId(18);  
       agent1.setAddress("北京市");  
       //模拟数据创建一个数据对象2  
       Agent agent2 = new Agent();  
       agent2.setAgentName("张三");  
       agent2.setAgentId(18);  
       agent2.setAddress("上海市");        
       List<Agent> userList = new ArrayList<Agent>();  
       userList.add(agent1);  
       userList.add(agent2);  
       return userList;  
   }  

  1. 返回json数据:[{"agentId"18,"agentName""张三","address""北京市"},{"agentId"18,"agentName":  "张三","address""上海市"}]  


在Controller需要返回一个对象的json数据格式 
/** 
     * 获取所有用户列表 
     */  
    @RequestMapping(value="getUserList",method=RequestMethod.GET)  
    public @ResponseBody Agent getUser(HttpServletRequest request, HttpServletResponse response){  
        //模拟数据创建一个数据对象1  
        Agent agent = new Agent();  
        agent.setAgentName("张三");  
        agent.setAgentId(18);  
        agent.setAddress("北京市");  
        return agent;  
    }  

返回json数据:{"agentId": 18,"agentName": "张三","address": "北京市"}
 
 上面的两种方式返回了list和对象的json数据格式,其他的比如map、string 等数据类型都可经过springMVC的处理返回json格式的数据


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值