0

I need to pass an array of an object to my controller. I'm trying to use @RequestBody like is described in the example here:

http://java2practice.com/2013/03/14/how-to-pass-json-object-string-or-jsonarray-string-from-javascript-to-spring-controller/

I've replicated the example in my project but I get a "bad request" error whenever I call it. I suspect this is because I'm using Spring 4 and Jackson 2 but I'm at a loss how to get it working.

Should this work in this environment? Is there a better way to do this?

2
  • Can you post the contents of the logs when the request fails? Commented Aug 29, 2014 at 16:11
  • @Steview try removing dataType: 'json', Commented Aug 29, 2014 at 17:08

1 Answer 1

1

Yes this should work in that environment (that is, using those versions of Spring and Jackson!). When faced with a mystery error like that my go-to is to crank up the log settings to TRACE for whatever library is failing.

I set up this example in my IDE (using Spring4 and Jackson2 as you are), and I immediately encountered a 400 "bad request" error as well. I cranked up the debug settings on org.springframework.web and com.fasterxml.jackson.core to find that Jackson2 was spitting out an exception:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of ca.ted.TestController$Person[] out of START_OBJECT token

This was because of the JSON being fed to the jQuery.ajax() call. I removed the "persons":{} object wrapping the array and sent only the array instead:

var arr = [ 
            {
                "firstName" : "Ramesh",
                "id" : "id1",
                "lastName" : "Kotha"
            }, 
            {
                "firstName" : "Sathish",
                "id" : "id2",
                "lastName" : "Kotha"
            } 
        ];

I did this but encountered the following exception:

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class ca.ted.TestController$Person]: can not instantiate from JSON object (need to add/enable type information?)

Turns out this was because Person was an inner class in my Controller. I moved it out to its own file, ca.ted.Person and all was well.

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

Actually I seem to have hit a new problem. The JSON is parsed successfully by the controller, but it always returns an error saying "not acceptable". The logs report: "Could not find acceptable representation"
Got it. This was because the example used the .htm extension which is apparently confusing my dispatchservlet. See: stick2code.blogspot.com.au/2014/03/…
Ah! Nice catch. Glad I was able to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.