简体   繁体   English

如何在Spring Boot Controller中读取两个JSON对象

[英]How to read two json objects in spring boot controller

I am new to java, i am trying to pass two json objects from ajax call to controller class... but i am getting below exception 我是Java的新手,我试图将两个json对象从ajax调用传递给控制器​​类...但是我遇到了以下异常

Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.controllers.EmployeeController.saveData(java.lang.String,com.entities.EmployeeData,org.springframework.validation.BindingResult)

Jquery Code: jQuery代码:

$("#saveData").submit(function(event) {          
    var data = [];
    var formData = {};
    var myJsonString;       
    var slab, lower;
    $("table tbody tr").each(function(index) {
        //alert(index);
       slab = $(this).find('.slab').val();
       lower = $(this).find('.lower').val();
      if(slab != undefined && lower != undefined){
        var form1 =new Object();
    form1.slab=slab;
    form1.lower=lower;
    data.push(form1);
      }         
    });
    var form = this;
    event.preventDefault();      
        $.each(this, function(i, v){
        var input = $(v);      
        formData[input.attr("name")] = input.val();
        });
    var url = "/Portal/SaveData";       
    ajaxCall(url,formData,data);

});
function ajaxCall(url,formData,data){   
    //alert("AjaxPost!!!"+url);
    // DO POST
    $.ajax({
        type : "POST",
        contentType : "application/json",
        url : url,
        data : JSON.stringify({formData:formData,value:data}),
        dataType : 'json',
        beforeSend: beforeSendHandler,
        success : function(response) {
            alert("Success");
            }else{          
                alert("else");
            }               
        },
        error : function(e) {
            bootbox.alert({ 
                  size: "small",
                  title: "ALERT",                   
                  message: "There seems to be some problem while Proccessing record!"
                })

        }
    });
}

Controller Method: 控制器方式:

@RequestMapping(value = "/SaveData", method = RequestMethod.POST)
public @ResponseBody String saveData(@RequestBody String value,@Valid @RequestBody EmployeeData emp, BindingResult result) {
System.out.println(" Creating!!!");
//logic here
}

Where is the mistake in ajax call or in controller file? Ajax调用或控制器文件中的错误在哪里? there is any other way to pass multiple json objects to controller class file? 还有其他方法可以将多个json对象传递给控制器​​类文件吗?

The @RequestBody annotation is expected to map to one and only one parameter in your method and to contain the entire contents of the incoming request. @RequestBody批注应映射到方法中的一个且只有一个参数,并包含传入请求的全部内容。

You should map all your form data into a single JSON object, then handle that as a single JSON object in the backend as well. 您应该将所有表单数据映射到单个JSON对象,然后在后端将其作为单个JSON对象进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM