springMVC的json交互

来源:互联网 发布:借花花网络贷款可靠吗 编辑:程序博客网 时间:2024/05/21 07:12

springmvc和客户端进行json交互的时候,如下过程;

 

 

较常用的是客户端请求格式:key/value,输出json串。

开发环境:需要添加jackson的依赖:

<dependency>            <groupId>com.fasterxml.jackson.core</groupId>            <artifactId>jackson-core</artifactId>            <version>2.5.0</version>        </dependency>        <dependency>            <groupId>org.codehaus.jackson</groupId>            <artifactId>jackson-mapper-asl</artifactId>            <version>1.9.13</version>        </dependency>

springmvc使用jackson包进行转换(@requestBody和@responsebody使用上面两个依赖进行串--》java对象和java对象--》串),配置json转换器:

<!--注解适配器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"><property name="messageConverters"><list><bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean></list></property></bean>


使用了mvc:annotation-driven就不需要配置上面bean.

请求key./value响应json代码:

<html><head>    <script type="text/javascript" src="/js/jquery-1.8.3.js"></script>    <script type="text/javascript">            //请求的是key/value,输出的是json            function responseJson(){                alert("dd");                $.ajax(                        {                    type:'post',                    //请求是key/value这里不需要指定contentType,因为默认就 是key/value类型                    //contentType:'application/json;charset=utf-8',                    //数据格式是json串,商品信                    data:'userName=phone&id=999',                    url:'/response',                    success:function(data){//返回json结果                        alert(data.userName);                    }                });            }           //请求的是json,输出json            function requestJson(){                $.ajax(                        {                            type:'post',                            contentType:'application/json;charset=utf-8',                            //数据格式是json串                            data:'{"userName":"phone","id":999}',                            url:'/requestJson',                            success:function(data){//返回json结果                                alert(data.userName);                            }                        });            }    </script></head><body><button id="requestJson" onclick="requestJson()" >requestJson</button><button id="responseJson" onclick="responseJson()" >responseJson</button></body></html>


controller代码:

package com.jd.jr.controller;import com.jd.jr.po.UserPo;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import java.io.UnsupportedEncodingException;/** * Created by guojiangjiang on 2015/8/8. */@Controllerpublic class TestJackson {    /**     *     *     * @return     * @throws UnsupportedEncodingException     */    //输出json,ajax请求为json串,要求将json转为java对象    @RequestMapping(value = "/requestJson")    public @ResponseBody UserPo getAppointment(@RequestBody UserPo userPo) throws UnsupportedEncodingException {        /*        * 在形参接残类型为pojo,直接将json串请求转为java对象        * 在参数类型前加注释@requestBody.        *        * 在接受了客户端ajax的json请求后,用RequestBody        * 将json串转为pojo.        * 处理完业务后返回处理的结果,要求输出完了转json格式,        * 返回的结果类型pojo,所以在返回类型的前面添加注释:        * @responseBody        * 总结:        * requestBody注解是将请求信息的json串转为java 对象        * responseBody注解是将返回的类型为java对象转为json串返回        *        * */        System.out.println("jakcson ...."+userPo);        return userPo;    }    @RequestMapping("/response")    public @ResponseBody UserPo responseBody(UserPo userPo ){        System.out.println("json data:"+userPo);        return userPo;    }}


在客户端可以通过data.userName,data.id的形式拿到返回的数据
 

 

 

0 0
原创粉丝点击