在Spring MVC Rest中处理JSon时如何处理POJO嵌套对象 [英] How to handle POJO nested objects when dealing with JSon in Spring MVC Rest

查看:116
本文介绍了在Spring MVC Rest中处理JSon时如何处理POJO嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何更好地处理Spring MVC中嵌套Java对象的JSon序列化/反序列化.

我的域模型如下:

  public class Cart {
        private String id;
        private Customer customerID;
        private Checkout checkoutID;
        private List<CartProduct> itemCatalogList;

        *** ... getters & setters ... ***


    }

   public class ProductCart {
        private String sku;
        private String color;
        private String sizeBase
        private int qty;

        *** ... getters & setters ... ***


    }

    public class Checkout {
        private String id;
        private String billingAddress;
        private String shippingAddress;
        private Cart cartID;

        *** ... getters & setters ... ***


    }

我在想的JSon是这样的:

结帐:

{
  "cart": {
    "$oid": "51f631cb84812abb04000006"
  },

  "shippingAddress" : "5h avenue - new york",  
  "billingAddress" : "5h avenue - new york"
}

购物车:

{
       "customer": {
      "$oid": "5174da574940368a9126e8dc"
      },
       "items_catalog": [
      {
        "sku": "00075161",
        "color": "ff99cc",
        "size_base": "IT_25",
        "qty": 3,
      },
      {
        "sku": "00075161",
        "color": "ff99cc",
        "size_base": "IT_27",
        "qty": 2,
      },
      {
        "sku": "00075161",
        "color": "ff99cc",
        "size_base": "IT_29",
        "qty": 1,
      }
}

假设这是可行的域模型& json文档,在春季如何从JSon开始创建结帐?

我的问题是我不知道如何在结帐& cart json,以便创建结帐&购物车Java Bean:

  • 有没有办法用Jackson来自动完成?

  • 还是我应该创建一种拦截器来处理例如checkout json,以便检索购物车,然后执行到POJO的映射?

(-还是第三种方式?)

非常感谢您提供任何建议.

解决方案

如果我对您的理解正确,那么您可以执行以下操作(我使用的是Spring 3.2.3.3.RELEASE& Jackson 1.9.12).

在您的applicationContext.xml中,您具有:

<bean id="jacksonMessageConverter"
          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter"/>
            </list>
        </property>
    </bean>

您有一个看起来像这样的Spring控制器:

package test;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/json")
public class JsonParsingController {
    private final static Logger log = Logger.getLogger(JsonParsingController.class);

    @RequestMapping(value = "/cart.do", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ResponseBody public CartResponse handleCart(@RequestBody Cart cart) {
    if (cart != null) {
        log.debug(cart);
    }

    return new CartResponse("OK!");
}
}

和三个POJO:

package test;

public class Cart {
    private String id;
    private Checkout checkoutID;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Checkout getCheckoutID() {
        return checkoutID;
    }

    public void setCheckoutID(Checkout checkoutID) {
        this.checkoutID = checkoutID;
    }

    @Override
    public String toString() {
        return "Cart{" +
                "id='" + id + '\'' +
                ", checkoutID=" + checkoutID +
                '}';
    }
}

package test;

public class Checkout {
    private String id;
    private String billingAddress;
    private String shippingAddress;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getBillingAddress() {
        return billingAddress;
    }

    public void setBillingAddress(String billingAddress) {
        this.billingAddress = billingAddress;
    }

    public String getShippingAddress() {
        return shippingAddress;
    }

    public void setShippingAddress(String shippingAddress) {
        this.shippingAddress = shippingAddress;
    }

    @Override
    public String toString() {
        return "Checkout{" +
                "id='" + id + '\'' +
                ", billingAddress='" + billingAddress + '\'' +
                ", shippingAddress='" + shippingAddress + '\'' +
                '}';
    }
}

package test;

public class CartResponse {
    private String result;

    public CartResponse(String result) {
        this.result = result;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }
}

然后,您可以在HTML页面中执行以下操作:

<script language="JavaScript" type="text/javascript">
    $(document).ready(function () {
        // Your data
        var arr = {
                    id: '51f631cb84812abb04000006',
                    checkoutID: {
                        id: '123456789',
                        "shippingAddress" : "5h avenue - new york",
                        "billingAddress" : "5h avenue - new york"
                    }
                  };
        $.ajax({
            url: '/json/cart.do',
            type: 'POST',
            data: JSON.stringify(arr),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            async: false,
            success: function (msg) {
                alert(msg.result);
            }
        });
    });
</script>

至少对我来说-它有效:)

I'm trying to figure out how to better deal with JSon serialization/deserialization of nested Java objects in Spring MVC.

My domain model is the following:

  public class Cart {
        private String id;
        private Customer customerID;
        private Checkout checkoutID;
        private List<CartProduct> itemCatalogList;

        *** ... getters & setters ... ***


    }

   public class ProductCart {
        private String sku;
        private String color;
        private String sizeBase
        private int qty;

        *** ... getters & setters ... ***


    }

    public class Checkout {
        private String id;
        private String billingAddress;
        private String shippingAddress;
        private Cart cartID;

        *** ... getters & setters ... ***


    }

The JSon I was thinking is something like this:

checkout:

{
  "cart": {
    "$oid": "51f631cb84812abb04000006"
  },

  "shippingAddress" : "5h avenue - new york",  
  "billingAddress" : "5h avenue - new york"
}

cart:

{
       "customer": {
      "$oid": "5174da574940368a9126e8dc"
      },
       "items_catalog": [
      {
        "sku": "00075161",
        "color": "ff99cc",
        "size_base": "IT_25",
        "qty": 3,
      },
      {
        "sku": "00075161",
        "color": "ff99cc",
        "size_base": "IT_27",
        "qty": 2,
      },
      {
        "sku": "00075161",
        "color": "ff99cc",
        "size_base": "IT_29",
        "qty": 1,
      }
}

Assuming this is a viable domain model & json document, how in Spring I could create a checkout starting from a JSon?

My problem is that I don't know how to "explode" the $oid in the checkout & cart json in order to create checkout & cart Java Beans:

  • is there a way to do it automatically with Jackson?

  • or should I create a sort of Interceptor to handle a, for example, checkout json in order to retrieve the cart and then perform the mapping to the POJO?

(- or there is a 3rd way?)

Thanks a lot for any advice.

解决方案

If I understood you correctly, you could do something like this (I'm using Spring 3.2.3.RELEASE & Jackson 1.9.12).

In your applicationContext.xml you have:

<bean id="jacksonMessageConverter"
          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter"/>
            </list>
        </property>
    </bean>

You have Spring controller which looks like this:

package test;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/json")
public class JsonParsingController {
    private final static Logger log = Logger.getLogger(JsonParsingController.class);

    @RequestMapping(value = "/cart.do", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ResponseBody public CartResponse handleCart(@RequestBody Cart cart) {
    if (cart != null) {
        log.debug(cart);
    }

    return new CartResponse("OK!");
}
}

and three POJOs:

package test;

public class Cart {
    private String id;
    private Checkout checkoutID;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Checkout getCheckoutID() {
        return checkoutID;
    }

    public void setCheckoutID(Checkout checkoutID) {
        this.checkoutID = checkoutID;
    }

    @Override
    public String toString() {
        return "Cart{" +
                "id='" + id + '\'' +
                ", checkoutID=" + checkoutID +
                '}';
    }
}

package test;

public class Checkout {
    private String id;
    private String billingAddress;
    private String shippingAddress;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getBillingAddress() {
        return billingAddress;
    }

    public void setBillingAddress(String billingAddress) {
        this.billingAddress = billingAddress;
    }

    public String getShippingAddress() {
        return shippingAddress;
    }

    public void setShippingAddress(String shippingAddress) {
        this.shippingAddress = shippingAddress;
    }

    @Override
    public String toString() {
        return "Checkout{" +
                "id='" + id + '\'' +
                ", billingAddress='" + billingAddress + '\'' +
                ", shippingAddress='" + shippingAddress + '\'' +
                '}';
    }
}

package test;

public class CartResponse {
    private String result;

    public CartResponse(String result) {
        this.result = result;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }
}

Then in your HTML page you can do something like this:

<script language="JavaScript" type="text/javascript">
    $(document).ready(function () {
        // Your data
        var arr = {
                    id: '51f631cb84812abb04000006',
                    checkoutID: {
                        id: '123456789',
                        "shippingAddress" : "5h avenue - new york",
                        "billingAddress" : "5h avenue - new york"
                    }
                  };
        $.ajax({
            url: '/json/cart.do',
            type: 'POST',
            data: JSON.stringify(arr),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            async: false,
            success: function (msg) {
                alert(msg.result);
            }
        });
    });
</script>

At least as for me - it works :)

这篇关于在Spring MVC Rest中处理JSon时如何处理POJO嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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