JSON ajax POST to Spring Portlet Controller @ResourceMapping conversion issue

假装没事ソ 提交于 2019-12-11 20:37:18

问题


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);
    });

Controller:

@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());
}

Model:

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

Also everything works fine if the JSON is changed to:

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

Error in controller:

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]

My issue is very similar to Post Nested Object to Spring MVC controller using JSON and Spring Portlet Jquery Ajax post to Controller

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

Thanks


回答1:


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:

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)
    };

Where fieldGroupList is an array in javascript.

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");
        }
    }
}


来源:https://stackoverflow.com/questions/31546689/json-ajax-post-to-spring-portlet-controller-resourcemapping-conversion-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!