JSON ajax POST到Spring Portlet Controller @ResourceMapping转换问题

编程入门 行业动态 更新时间:2024-10-25 15:26:47
本文介绍了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转换问题

本文发布于:2023-10-07 17:28:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470025.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:POST   ajax   JSON   Spring   ResourceMapping

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!