spring mvc 传递复杂的对象

在spring mvc 传递简单对象时候,是比较简单的,

但是,在传递List 等,集合时,初学者,可能有点麻烦,


http://blog.csdn.net/lutinghuan/article/details/46820023


http://blog.csdn.net/wabiaozia/article/details/50803581


http://blog.csdn.net/u011781521/article/details/77586688

http://blog.csdn.net/jhq2904982765/article/details/77141618


上面是我,解决各种问题.所需啊哟的blg


其中,前两篇,可以解决大部问题,

最后,一篇,可以解决,全部问题

,接下来,我描述下我的问题

这是我car 对象的属性,其中有两个 SET  集合

https://gitee.com/echo_yi/cars/blob/master/src/main/java/com/org/cars/domain/Car.java


对于这个问题,前段采用json 传输,但是,注意格式


jQuery("form").serialize(); //"username=&password="    String
jQuery("form").serializeArray(); //[{name:"username",value:""},{name:"password",value:""}]  shuzu
jQuery("form").serializeObject(); //{username:"",password:""}  
JSON.stringify(jsondata)

$.toJSON(customerArray), 

要理解,每一种的状态,之前,我直接就是直接序列  serialize(),导致出错



这是我的 js

$(document).ready(function(){
    submitUserList_3();
    function submitUserList_3() {
        var car ={ "id" : 1, "name" : "2018 730Li 领先型 M运动套装", "price" : 28.88, "brand" : "奔驰", "model" : "7", "mileage" : 11.4, "vehicleOfAge" : 5, "numberOfHouseholds" : 0, "emissionOfStandard" : "国2", "checkOfexpires" : "2017-11-29", "levelOfType" : "家用", "displacement" : 2.0, "nationality" : "德国", "bodyOfType" : "二厢", "driverOfType" : "后驱", "pictures" : [ { "id" : 4, "type" : "interior", "address" : "/0395b396-8b7c-4d1b-a2c9-eb8aea2ee30b.png" }, { "id" : 1, "type" : "details", "address" : "/50b860fd-9f4b-4a0e-aef0-f5654bc7262f.png" }, { "id" : 3, "type" : "details", "address" : "/0a830b74-5073-4d1e-8436-f431d52ba6c0.png" }, { "id" : 5, "type" : "details", "address" : "/82e27b36-b81c-4ac2-9779-88f6c02c68cb.png" }, { "id" : 9, "type" : "appearance", "address" : "/ba3698dc-cef4-405e-9226-3f63e2d55985.png" }, { "id" : 7, "type" : "appearance", "address" : "/b9548679-f8cf-4d8a-8180-3669a0591189.png" }, { "id" : 8, "type" : "interior", "address" : "/244cd446-b25f-4673-9325-61e650166467.png" }, { "id" : 11, "type" : "appearance", "address" : "/d1dd4cc8-470d-45b4-a158-14ecc9c36077.png" }, { "id" : 2, "type" : "interior", "address" : "/4bd801f9-9c7c-47da-a53e-4729f2a83639.png" }, { "id" : 10, "type" : "interior", "address" : "/7e82bc86-f331-4b57-9d3c-51acd56d82e9.png" }, { "id" : 12, "type" : "appearance", "address" : "/2a8be682-166c-473d-891f-0e027053799b.png" }, { "id" : 6, "type" : "details", "address" : "/d401ed1f-867f-4069-ba27-4541005a6b49.png" } ], "cardOfTime" : "2017-11-29", "strongOfInsurance" : "2017-11-29", "businessOfInsuranceOfExpires" : "2017-11-29", "interpretationOfCar" : [ { "id" : 1, "address" : "/a3f69b0f-0cca-42dd-922a-b2384ae76cb0.png", "interpretationOfInfo" : "upipoijououououoiuoiuoiuouoiuoiuoiuoiui " } ], "cardOfAddress" : "广州", "transmissionOfType" : "手动", "fuelOfType" : "混合", "seatOfNumber" : 3, "releaseOfTime" : "2017-11-29" };
        $.ajax({
            url: "/twoone",
            type: "POST",
            contentType : 'application/json;charset=utf-8', //设置请求头信息
            dataType:"json",
            //data: JSON.stringify(customerArray),    //Json对象序列化成Json字符串,JSON.stringify()原生态方法
            // data: $.toJSON(customerArray),            //Json对象序列化成Json字符串,toJSON()需要引用jquery.json.min.js
            // data:$("#put").serialize(),
            // data:"model="+ddd,
            data:JSON.stringify(car),
            success: function(data){
                console.log(data);
                console.log($("#put").serialize());
                console.log(JSON.stringify(car));
            },
            error: function(res){
                // alert(res.responseText);
                console.log(res.responseText);
            }
        });
    }

});



其中,对于car 对象的数据,我是采用,先从后台输出一个@Responsbody car,

直接运用 谷歌的postman 获取数据,

然后放在js 里面(因为对js 不熟,只好采用简单的方法)



这是我的controller

    @PostMapping(value="/twoone",produces = MediaType.APPLICATION_JSON_VALUE)
//    @Time
//    @RequestMapping(value = "/twoone")
    @ResponseBody
    public Car u(@RequestBody Car car){
        System.out.println("-----------------");
        System.out.println(car.getName());
//        return carRepository.findOne(1);
        return car;
    }

特别是

@PostMapping(value="/twoone",produces = MediaType.APPLICATION_JSON_VALUE)

http://blog.csdn.net/jhq2904982765/article/details/77141618

声明文件,这个特别重要

,问题,解决

建议看下orgjson




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值