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

来源:互联网 发布:淘宝黑莓 编辑:程序博客网 时间:2024/05/13 22:38
 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.xsdhttp://www.springframework.org/schema/task   http://www.springframework.org/schema/task/spring-task-4.0.xsdhttp://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;    }
    返回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
原创粉丝点击