172015May

Sending Complex Object To ATG REST MVC

Sending complex object to ATG REST MVC is a big bummer for developer when try to send complex data structure like atg-rest-class-type and atg-rest-values in the input payload. As these keyword are necessary so that REST architecture understands it correctly and map to corresponding objects.

Here I am gonna give you a simple example how to make that complex input payload structure. The input payload here I am using is the payload for addMultipleItemsToOrder. Below is the payload

{"addItemCount": 3, "items": {"atg-rest-class-type":"java.util.ArrayList","atg-rest-values": [{"atg-rest-class-type":"atg.commerce.order.purchase.AddCommerceItemInfo", "catalogRefId":"xsku1043","productId": "xprod1015","quantity": 1},{"atg-rest-class-type":"atg.commerce.order.purchase.AddCommerceItemInfo","catalogRefId":"xsku60325","productId": "xprod40028","quantity":2},{"atg-rest-class-type":"atg.commerce.order.purchase.AddCommerceItemInfo"\,"catalogRefId":"xsku1001","productId": "xprod1001","quantity": 3} ]}}

Steps needed to make this complex structure

  • Classes with nested objects and corresponding member variables.
  • Member variable with atg-rest-class-type name but java doesn’t allow this variable name.
  • Library to convert Java objects in to JSON ( Jackson we are going to use).

Classes Structure

  1. Wrapper Class
    • addItemCount (int)
    • items (Another Class)
  2. Items class
    • atg-rest-class-type (string)
    • atg-rest-values (list contain the other class objects)
  3. List Objects Class
    • atg-rest-class-type (string)
    • catalogRefId
    • productId
    • quantity

POJO Classes

import org.codehaus.jackson.annotate.JsonProperty;
public class PayloadWrpper
{
	private int addItemCount;
	private Items items;
	
	//setter getter
}
import org.codehaus.jackson.annotate.JsonProperty;
public class Items
{
	@JsonProperty(value="atg-rest-class-type")
	private String mapperClass = "java.util.ArrayList";
	@JsonProperty(value="atg-rest-values")
	private List<Item> itemList;
	
	//setter getter
}
import org.codehaus.jackson.annotate.JsonProperty;
public class Item
{
	@JsonProperty(value="atg-rest-class-type")
	private String mapperClass = "atg.commerce.order.purchase.AddCommerceItemInfo";
	private String catalogRefId;
	private String productId;
	private int quantity;
	
	//setter getter
}

The above classes defined uses @JsonProperty annotation because Java does not allow the property name like atg-rest-class-type so Jackson library provide this annotation to change property name when converting these Java Objects to JSON.

Now when we convert the wrapper class into JSON we will get exact structure which is needed to make the same way ATG REST MVC understands.