【发布时间】:2015-04-02 20:03:05
【问题描述】:
我正在寻找如何在 Spring Portlet MVC 中的 @ResourceMapping 中解释 JSON 参数。当我添加@RequestBody 时,我收到消息:不支持@RequestBody ......真的卡在这个上了。
我有这个:
查看面:
<portlet:resourceURL var="getTest" id="ajaxTest" ></portlet:resourceURL>
<p>
<button onClick="executeAjaxTest();">Klik mij!</button>
<button onClick="$('#ajaxResponse').html('');">Klik mij!</button>
</p>
<p>
<h3>Hieronder het antwoord:</h3>
<h4 id="ajaxResponse"></h4>
</p>
<script>
function executeAjaxTest() {
var jsonObj = {
user: "Korneel",
password: "testpassword",
type: {
testParam: "test",
}
}
console.debug(JSON.stringify(jsonObj));
$.ajax({
dataType: "json",
contentType:"application/json",
mimeType: 'application/json',
url:"<%=getTest%>",
data:JSON.stringify(jsonObj),
success : function(data) {
$("#ajaxResponse").html(data['testString']);
}
});
}
</script>
控制器端:
@ResourceMapping(value="ajaxTest")
@ResponseBody
public void ajaxTestMethod(ResourceRequest request, ResourceResponse response) throws IOException, ParseException {
LOGGER.debug("ajax method");
JSONObject json = JSONFactoryUtil.createJSONObject();
json.put("testString", "Ik ben succesvol verstuurd geweest!");
response.getWriter().write(json.toString());
}
我如何使用 spring 魔法将这个 JSON 数据自动映射到我自己的模型? 注意:它是 Spring Portlet MVC,而不是常规 Spring MVC..
【问题讨论】:
标签: ajax json spring spring-portlet-mvc