5 November 2012

How to solve problem with Spring 3 that it return 406 instead of json object ?

Solution works for Spring 3.1

STORY

 Spring 3 come with  REST integration ,so  for example .You can  ask  Spring 3 kindly to  return JSON object? Awesome.
So you follow one of tutorials* ,but when you run example from tutorial, you see:

HTTP Status 406 -

type Status report
message
description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().

Apache Tomcat/7.0.12



which not exactly what you expected to see.
First .What Error 406 is ?

Error 406  means "Web server detects, that the data (JSON Object in our case) it wants to return is not acceptable to the client (your web browser),so  it returns a 406 error code instead.

For more detail about Error 406 : look here:  http://www.checkupdown.com/status/E406.html

According to above page:
"This error occurs very infrequently in Web browsers, because most browsers will accept any data returned from the Web server."

How lucky you are to see very very  infrequently error like that?

SOLUTION:

You just forgot add add Jackson library to your libraries

Jar can be found on this page: http://jackson.codehaus.org/

(of course normaly you will add to your maven dependencies)


Sources:

(Error 406)  http://www.checkupdown.com/status/E406.html

(Jackson JSON processor ) http://jackson.codehaus.org/

Useful tutorial for Spring 3 and JSON can be found here:
http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

2 comments:

  1. I'm getting the same error even after adding the jackson jar.

    ReplyDelete
    Replies
    1. For 2 cases,it helps.

      As far as I know.It should solve problem ... however ... Double check config for spring/server/IDE/web browser
      Is your deployed app has jar as well ?Is your json is made by jackson one not other jar?


      Or is .. .another reason,which i am not aware.
      Check stackoverflow ...

      Good luck!

      Delete