springMVC参数绑定JSON类型的数据 springMVC学习(11)-json数据交互和RESTful支持

https://www.cnblogs.com/tenWood/p/6736700.html?utm_source=itdadao&utm_medium=referral

需求就是:

现在保存一个Student,并且保存Student的friend,一个student会有多个朋友,这里要传递到后台的参数是:

复制代码
var friends = new Array();
    var o1 = {
        "family": "大宅门",
        "otherName": "lisi",
        "desc": "亲密无间的好朋友"
    }

    var o2 = {
        "family": "军大院",
        "otherName": "wangwu",
        "desc": "关系一般"
    }

    friends.push(o1);
    friends.push(o2);

//要传递的参数:id=1 name=zhangsan age=10是要保存的student信息,并且friends是student的朋友信息;
data:{
            "id": 1,
            "name": "zhangsan",
            "age": 10,
            "friends":friends
        },
复制代码

一、包装类型来接收JSON类型的传参:                                                                                           

方法一其实就是 springMVC学习(11)-json数据交互和RESTful支持 中接收json数据;只不过包装了另一个po

前台代码,ajax传递数据:

复制代码
<input type="button" onclick="saveFriend()" value="保存friend" />
<script type="text/javascript">
/**
 * 保存朋友
 */
function saveFriend(){
    var friends = new Array();
    var o1 = {
        "family": "大宅门",
        "otherName": "lisi",
        "desc": "亲密无间的好朋友"
    }

    var o2 = {
        "family": "军大院",
        "otherName": "wangwu",
        "desc": "关系一般"
    }

    friends.push(o1);
    friends.push(o2);
    
    //要发送的参数
    var params = {
        "id": 1,
        "name": "zhangsan",
        "age": 10,
        "friends":friends    
    };
    
    $.ajax({
        type: 'post',
        url: '${pageContext.request.contextPath }/saveStudent',
        contentType:'application/json;charset=utf-8',
        dataType: 'json',
        data: JSON.stringify(params),
        success: function(data){
            console.log(data);
        }
    });
}
</script>
复制代码

Controller中接受这个参数:

复制代码
/**
     * 保存朋友
     */
    @RequestMapping("/saveStudent")
    @ResponseBody
    public void saveStudent(@RequestBody StudendAndFriend studendAndFriend){
        System.out.println("接收参数friends--->> " + studendAndFriend);
    }
复制代码

接收参数的包装类:

StudendAndFriend.java:

复制代码
package com.cy.po;

import java.util.List;

/**
 * 接收参数包装类
 * @author chengyu
 *
 */
public class StudendAndFriend {
    private int id;
    private String name;
    private int age;
    private List<Friend> friends;
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public List<Friend> getFriends() {
        return friends;
    }
    public void setFriends(List<Friend> friends) {
        this.friends = friends;
    }
    
    
}
复制代码

Friend.java:

复制代码
package com.cy.po;

public class Friend {
    private String family;
    private String otherName;
    private String desc;
    
    
    public String getFamily() {
        return family;
    }
    public void setFamily(String family) {
        this.family = family;
    }
    public String getOtherName() {
        return otherName;
    }
    public void setOtherName(String otherName) {
        this.otherName = otherName;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    
    @Override
    public String toString() {
        return "Friend [family=" + family + ", otherName=" + otherName
                + ", desc=" + desc + "]";
    }
    
}
复制代码

后台成功接收参:

注意点:

ajax传参:

contentType:'application/json;charset=utf-8'; //contentType为application/json

data: JSON.stringify(params),           //传的是字符串;

方法二、                                              

传递String类型的json字符串过来,后台接收后,进行JSONArray解析成对应的List<Object>;

这里使用fastjson-1.2.2.jar包来对json字符串转化为javabean类型;

前端发送请求:

复制代码
<input type="button" onclick="saveFriend2()" value="保存friend2" />
<script type="text/javascript">
/**
 * 保存朋友2
 */
function saveFriend2(){
    var friends = new Array();
    var o1 = {
        "family": "大宅门",
        "otherName": "lisi",
        "desc": "亲密无间的好朋友"
    }

    var o2 = {
        "family": "军大院",
        "otherName": "wangwu",
        "desc": "关系一般"
    }

    friends.push(o1);
    friends.push(o2);
    
    friends = JSON.stringify(friends);
    
    $.ajax({
        type: 'post',
        url: '${pageContext.request.contextPath }/saveStudent2',
        dataType: 'json',
        data: {
            "id": 1,
            "name": "zhangsan",
            "age": 10,
            "friends":friends    
        },
        success: function(data){
            console.log(data);
        }
    });
}
</script>
复制代码

Controller接收这个参数:

复制代码
   //用的是fastjson 

    import com.alibaba.fastjson.JSONArray;
    import com.cy.po.Friend;


  /** * 保存朋友2 */ @RequestMapping("/saveStudent2") @ResponseBody public void saveStudent2(int id, String name, int age, String friends){ List<Friend> frientList = JSONArray.parseArray(friends, Friend.class); System.out.println("接收参数friends--->> " + frientList); }
复制代码

可以看到前端发送参数:

controller中经过json解析为javabean:

猜你喜欢

转载自www.cnblogs.com/zhanglijun/p/9112957.html