Showing posts with label jackson. Show all posts
Showing posts with label jackson. Show all posts

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/

27 March 2012

how to get data from sub node in jackon library for json ?

Jackson is an AWESOME multi-purpose Java library for processing JSON data format.

STORY:
It easy to take this example:

let's say that we have a following json:
{"ground":
[{
"overground":"birds",
"underground":"worms",
}],
"house":"cottage}

to get your house, you need:

pictureNode.path("house").asText();

PROBLEM:
how to get overground that is inside ground ?

SOLUTION:
 pictureNode.path("ground").path("underground").asText();