JSON ajax POST到Spring Portlet Controller @ResourceMapping转换问题 [英] JSON ajax POST to Spring Portlet Controller @ResourceMapping conversion issue

查看:227
本文介绍了JSON ajax POST到Spring Portlet Controller @ResourceMapping转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSON数据发布到Spring Portlet Controller方法. Modelattribute对象是嵌套的.这是JSON:

I'm trying to post JSON data to Spring Portlet Controller method. The Modelattribute object is nested. Here is the JSON:

$(document).on({    
  change: function() {
...
...
formData = {
            "name": "Demo",
            "id": 724,
            "periodId": 2015,
            "orgId": 4,
            "fieldGroupList": [{
                "name": "instructions",
                "label": "Instructions",
                "fieldList": [{
                    "name": "INSTRUCTION",
                    "instructionList": [{
                        "instructionText": "All enabled fields are required for completion of this screen."
                    }],
                    "type": "INSTRUCTION"
                }]
            }]
        };

Ajax:

$.ajax({
        async: "false",
        global: "false",
        url: validateURL,
        type: "POST",
        data: formData,
        dataType: "json"        
    }).done(function(json){
        console.log(json);
    }).fail(function(jqXHR, textStatus, error) {
        console.log("responseText: "+jqXHR.responseText);
    });

控制器:

@ResourceMapping(value = "validateURL")
    public void validate(@ModelAttribute(value = "formData") Measure measure,           
                        BindingResult bindingResult, ResourceRequest request, ResourceResponse response, ModelMap model) throws Exception {
        System.out.println("ab:::"+measure.getId());
}

型号:

public class Measure
{
    private String name;
    private List<MeasureFieldGroup> fieldGroupList = new ArrayList<MeasureFieldGroup>();
...
}

如果将JSON更改为:

Also everything works fine if the JSON is changed to:

formData = {
                "name": "Demo",
                "id": 724,
                "periodId": 2015,
                "orgId": 4              
            };

控制器错误:

org.springframework.beans.InvalidPropertyException: Invalid property 'fieldGroupList[0][fieldList][0][instructionList][0][instructionText]' of bean class abc.measures.base.Measure]: Illegal attempt to get property 'fieldGroupList' threw exception; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'fieldGroupList[0][fieldList][0][instructionList][0][instructionText]' of bean class [abc.measures.base.Measure]: Property referenced in indexed property path 'fieldGroupList[0][fieldList][0][instructionList][0][instructionText]' is neither an array nor a List nor a Set nor a Map; returned value was [abc.measures.base.MeasureFieldGroup@72fd67c]

我的问题与 使用JSON将嵌套对象发布到Spring MVC控制器 Spring Portlet Jquery Ajax发布到Controller

但是由于Spring Portlet而不能使用ResponseBody和RequestBody.任何帮助将不胜感激.

but can't use ResponseBody and RequestBody due to Spring Portlet. Any help would be appreciated a lot.

谢谢

推荐答案

当我尝试将JSON数组绑定到Spring portlet MVC中的模型时,我遇到了这个问题.我不知道问题出在春天,还是我构建JSON数组的方式.

I have had this problem when I try to bind JSON arrays to my model in Spring portlet MVC. I don't know whether the problem is spring or it is the way I build my JSON array.

我找到的解决方案是这样:

The solution that I found was this:

使用JSON.stringify将对象(在本例中为数组)转换为JSON格式的字符串.例如:

Use JSON.stringify to turn an object (in this case an array) into a string with a JSON format. For example:

formData = {
        "name": "Demo",
        "id": 724,
        "periodId": 2015,
        "orgId": 4,
        "fieldGroupString": JSON.stringify(fieldGroupList)
    };

fieldGroupList是javascript中的一个数组.

Where fieldGroupList is an array in javascript.

然后,您可以使用jackson库的ObjectMapper类将JSON字符串转换为模型中的对象列表.

Then, you can use the ObjectMapper class of the jackson library to turn the JSON string into a list of objects in your model.

public void setFieldGroupString(String fieldGroupString) {
    if(fieldGroupString != null){
        ObjectMapper mapper = new ObjectMapper();
        fieldGroupList = new ArrayList<MeasureFieldGroup>();
        try {
            fieldGroupList = mapper.readValue(fieldGroupString,
                        new TypeReference<List<MeasureFieldGroup>>() {
                        });
        } catch (Exception e) {
            logger.debug("Error in mapper");
        }
    }
}

这篇关于JSON ajax POST到Spring Portlet Controller @ResourceMapping转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆