将Ajax POST发送的JSON对象映射到Spring MVC模型 [英] Mapping JSON Object send by Ajax POST to Spring MVC Model

查看:233
本文介绍了将Ajax POST发送的JSON对象映射到Spring MVC模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,朋友,我从2天开始研究此问题,但无法解决.我希望stackoverflow可以对我有所帮助.

Hello Friends i am working on this problem from 2 days and not been able to solve. i Hope that stackoverflow could help me.

问题:我已经通过ajax发送了JSON对象,在后端我有一个类(名为:SalesCommandObject),其中包含其他模型的对象,这些方法的getter和setter. 然后尝试将类型为"SalesCommandObject"的JSON对象发送到控制器方法.这样就可以将json数据映射到模型数据中.

Problem: I have sent JSON object through ajax, at backend i have a class(named: SalesCommandObject) containing objects of other models, getters and setters of these. then trying to send an JSON object of type "SalesCommandObject" to controller method. so that the json data can be mapped into model data.

但是服务器抛出错误:"400错误的请求:客户端发送的请求在语法上不正确".

But the server throws error: "400 Bad Request :The request sent by the client was syntactically incorrect".

我正在发布整个代码.请检查并帮助我.

I am posting the entire code. Please Check and Help Me out.

//Ajax POST的代码:

// Code for Ajax POST:

  var salesCommandObject = {};
        salesCommandObject.CustomerInfo =
                {
                "address1": "Address_1",
                "city": "City",
                "pin": "PIN"
                };
        salesCommandObject.SalesModel = 
                {
                "locality":'Loc1',
                "shippingType":'Regular',
                "shippingCost":20
                };

               $.ajax
               ({
                  type: "POST",
                  dataType : 'json',
                  async : true,     
                  url: "http://localhost:8080/OnlineStore/kmsg/grocery/SaveSalesOrder",
                  data : JSON.stringify(salesCommandObject),
                  contentType: "application/json; charset=utf-8"
                  }).done(function(data,type,xml)
                            {
                              alert("result");
                              console.log(data);
                            }).fail(function()
                                      {
                                alert("Something Bad Happened, Service failed");
                          })

//控制器接收JSON对象的代码:

// Code of Controller Receiving JSON Object:

@RequestMapping(value = "/SaveSalesOrder", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)  
    public @ResponseBody String SaveCustomerOrder(@RequestBody SalesCommandObject salesCommandObject) throws Exception 
    {       
        CustomerModel cust = salesCommandObject.getCustomerInfo();
        SalesModel sale = salesCommandObject.getLocality();
        System.out.println(cust.getAddress1());
        System.out.println(sale.getLocality());
        return "Success";
    }

//类Model salesCommandObject的代码

//Code of class Model salesCommandObject

public class SalesCommandObject 
{
   private CustomerModel            CustomerInfo = new CustomerModel();
   private List<SalesItemsModel>    salesData ;
   private SalesModel               salesModel = new SalesModel();
   private SalesDeliverySlotsModel  salesDelSlotsModel = new SalesDeliverySlotsModel();
   private List<ItemsForSaleModel>  itemsforSale ;


      // getters and setters here//
}

推荐答案

朋友们我已经解决了这个问题:

Friends i have solved this problem:

我没有在模型中声明默认构造函数,这就是为什么我通过ajax发送的数据类型与接收端控制器参数中的数据类型不匹配的原因.另外,pom.xml中的杰克逊版本未正确设置. 必须记住的几件事是: Json对象的键值必须与设置的方法名称匹配: 例如,如果:Json data is {"key1":"value","keyTwo":"value123"} 然后是二传手:

I haven't declared the default constructors in the Models that's why type of data i was sending through ajax was not matching the data type in controller parameter at receiving end. Also the jackson version in pom.xml not set correctly. Few things that must be kept in mind is: The key value of the Json Objects must match the set method name: For example if: Json data is {"key1":"value","keyTwo":"value123"} then setters:

setKey1(){} setKeyTwo(){}

setKey1(){} setKeyTwo(){}

在所有使用的模型类中也必须有公共默认构造函数,我们将在其中映射我们的json数据.

Also there must be public default constructor in all the used model classes, in which we are going to map our json data.

谢谢.

这篇关于将Ajax POST发送的JSON对象映射到Spring MVC模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆