简体   繁体   English

与Spring MVC控制器绑定JSON数据时,出现“错误不受支持的媒体类型”

[英]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.

相关问题 在Spring MVC控制器中获取不受支持的媒体类型 - getting unsupported media type in spring mvc controller 从Jsp向Rest Controller发送Json数据时获取415(不支持的媒体类型错误) - Getting 415(Unsupported media type error) while sending the Json data from Jsp to Rest Controller 在使用带有json的jquery ajax的Spring MVC中,出现错误:“不支持的媒体类型” - In Spring MVC using jquery ajax with json, getting error : “unsupported media type” JSON plus spring mvc 3.2 error 415(不支持的媒体类型) - JSON plus spring mvc 3.2 error 415 (Unsupported Media Type) 向控制器发送表格数据时,出现415不支持的媒体类型错误 - Am getting 415 unsupported media type error while sending form data to controller Spring MVC中不支持的媒体类型 - Unsupported Media Type in Spring MVC 当我将json发送到spring控制器时,415不支持的媒体类型 - 415 Unsupported Media Type when I send json to spring controller 尝试使用Ajax请求从Spring控制器返回视图时,出现415不支持的媒体类型错误 - Getting 415 Unsupported Media Type Error when trying to return a view from a spring controller using an Ajax Request 使用angularjs将json发送到spring控制器时,浏览器控制台中出现不受支持的媒体类型错误 - Unsupported Media Type error in browser console when sending json using angularjs to spring controller AJAX-Spring MVC中不支持的媒体类型错误 - Unsupported Media Type Error in AJAX-Spring MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM