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.