deserialize nested JSON objects in Spring MVC with Jackson - 26 May 2013


NOTE: I had to use Jackson 2.2 to get this working. Jackson 1.9 did not cut it.

API needs

The VenueObj has a single LocationObj field, called location. The LocationObj field needs to be set when deserializing a full VenueObj JSON POST payload, and the location field needs to be serialized when the VenueObj is serialized.

VenueController.java POST mapping - Controller

@RequestMapping(value = RequestMappingConstants.kApiVenues, method = RequestMethod.POST)
protected @ResponseBody long putVenue(@RequestBody final VenueObj theVenueObj, final Principal thePrincipal, final HttpServletResponse response);

Note the annotation driven mapping. May need to wire up your spring-dispatcher-servlet.xml.

VenueObj.java constructor - JSON de/serialization and ORM

public VenueObj(@JsonProperty(VenueObj.NAME) final String theName,
                @JsonProperty(VenueObj.LOCATION) final LocationObj theLocation,
                @JsonProperty(VenueObj.PHONE) final String thePhone,
                @JsonProperty(VenueObj.WEB) final String theWeb) {
    this.name = theName;
    this.location = theLocation;
    this.phone = thePhone;
    this.web = theWeb;

All other public constructors should be annotated with @JsonIgnore to prevent the Jackson process from attempting to deserialize with the incorrect one and spitting up.

LocationObj.java getters/setters - JSON de/serialization and ORM

public float getLat() {
    return this.lat;

public float getLon() {
    return this.lon;


public void setLat(final float theLat) {
    this.lat = theLat;

public void setLon(final float theLon) {
    this.lon = theLon;

The LocationObj class is a constructor-less cacophony of getters and setters, with a few private data manipulation methods.


And now the service should be able to make sense of incoming JSON like:

    "name":"Eldora Mountain Resort",
    "phone":"(303) 440-8700",

Further Reading

332 words. Post tags: JSON, Jackson, and Java.

Post content is written by Jason Zerbe and licensed CC BY-NC 3.0.