[英]getting “error Unsupported Media Type” while binding JSON data with spring mvc controller
I am capturing a hierarchical data using Jquery based Jstree library.Data is in a JSON format, I want to capture and bind this data to my bean class(JstreeJson.java).Here is what I have tried so far.. 我正在使用基于Jquery的Jstree库捕获分层数据。数据是JSON格式,我想捕获此数据并将其绑定到我的bean类(JstreeJson.java)。这是到目前为止我尝试过的。
Ajax call : Ajax通话:
function getJSON() {
var jstree = $('#jstree1').jstree(true).get_json('#', {flat:true});
console.log(JSON.stringify(jstree));
$.ajax({
type:"POST",
url:"createObjective",
data : { jstree: jstree },
dataType :"json",
success : function(result) {
console.log(jstree);
console.log(result);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus, errorThrown);
}
});
}
console.log output: console.log输出:
[{"id":"j1_1","text":"Simple root node","icon":true,"li_attr":{"id":"j1_1"},"a_attr":{"href":"#","id":"j1_1_anchor"},"state":{"loaded":true,"opened":false,"selected":false,"disabled":false},"data":{},"parent":"#"}]
controller 调节器
@RequestMapping(value="/createObjective",method=RequestMethod.POST)
public @ResponseBody String createObjective(@RequestBody JstreeJson jstree)
{
System.out.println(jstree);
return "done";
}
Bean class 豆类
public class JstreeJson
{
private String id;
private String text;
private String parent;
// getters and setter
}
I have tried adding consumes and Headers but it didnt have any effect on my output 我尝试添加消耗和标头,但这对我的输出没有任何影响
@RequestMapping(value="/createObjective",method=RequestMethod.POST,consumes="application/json",headers = "content-type=application/x-www-form-urlencoded")
Try this @RequestMapping:- 试试这个@RequestMapping:-
@RequestMapping(value="/createObjective" method = RequestMethod.POST,consumes= {"application/json;charset=UTF-8"}, produces={"application/json;charset=UTF-8"})
public @ResponseBody String createObjective(@RequestBody JstreeJson jstree)
{
System.out.println(jstree);
return "done";
}
Or you can keep @RequestMapping simple as bellow: 或者,您可以像下面这样简单地使用@RequestMapping:
@RequestMapping(value="/createObjective")
Spring will take care rest of the attributes of @RequestMapping depending on request. Spring将根据请求照顾@RequestMapping的其余属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.