JSON API with Spring 3.1.2 and Jackson 2.2.0

It took me a few hours to get everything working but I am quite happy at how simple it really is.

I read this blog post that had most of the instructions on how to do it: Spring MVC with JSON via Jackson 2.0

Norris mentions that you need to make sure that you have the jackson-databind dependency otherwise you will get a 406 error. I was getting a 406 error even with the jackson databind because I did not have the mvc:annotation-driven specified. I assumed I had it and was surprised that it was not there (lost an hour to something so trivial).

I was also not sure how to submit a JSON post to the API that I was creating to validate that it worked. I found a few example but the one that worked for me was this answer on stackoverflow: Parsing JSON into Java objects in spring-mvc.

The other testing I needed to do was what the JSON message format was to send to Spring. I tried something like:

{ objectName: [ {“field1”: “value”, “field2”: “value”} ] }

But that was giving me 400 errors.

The JSON message format that works is:

{ “field1”: “value1”, “field2”: “value2” }

Note that I am submitting only 1 object at a time to the API.

The last error that I encountered is that I had an internal class for the status (return object on submission) that was private. I got a 500 error when the spring bean was trying to access it so I switched to a protected class and everything is working.

I like the short code that Spring allows me to write to get this to work. Should make it quite easy to maintain.

Advertisements

Published by

m5c

Java developper that loves photography and good coffee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s