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 :)