2015-07-21 43 views
0

我試圖將JSON數據發佈到Spring Portlet控制器方法。 Modelattribute對象是嵌套的。這裏是JSON:JSON ajax POST到Spring Portlet控制器@ResourceMapping轉換問題

$(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({ 
     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更改爲一切工作正常:

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 [[email protected]] 

我的問題是非常相似的 Post Nested Object to Spring MVC controller using JSONSpring Portlet Jquery Ajax post to Controller

但不能使用ResponseBody和RequestBody由於春季的Portlet。任何幫助將不勝感激。

感謝

回答

1

當我試圖JSON數組綁定到我在春天的portlet MVC模式,我也有這個問題。我不知道問題是春天還是我構建JSON數組的方式。

,我發現該溶液是這樣的:

使用JSON.stringify打開一個對象(在這種情況下的陣列)與一個JSON格式的字符串。例如:

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

其中fieldGroupList是javascript中的數組。

然後,您可以使用jackson庫的ObjectMapper類將JSON字符串轉換爲模型中的對象列表。

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