This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

Spring JSON causes problems when the Spring framework is upgraded from 3.0.2 to 3.2.0Page Title Module

Spring JSON causes problems when the Spring framework is upgraded from 3.0.2 to 3.2.0

Dec 28th, 2012, 09:21 PM

I was working with a web application using the Spring framework version 3.0.2 along with Hibernate (NetBeans 6.9.1). Later I came to know that there was one of bugs that was causing problems in uploading multiple files. Therefore, I upgraded the Spring version to (3.2.0).

With the earlier version 3.0.2, AJAX was working fine with Jackson 1.9.8 (its download page) but with the later version (3.2.0), everything works fine but AJAX calls alert an error everywhere in the JavaScript code.

Google chrome shows this error

Failed to load resource: the server responded with a status of 406 (Not Acceptable)

There is a simple scenario at one place when one of the countries is selected in the country select box, the corresponding state list is retrieved from the Spring controller along with DAO. The method which is mapped with a URL in the Spring controller is as follows,

The foreach loop is just for the sake of demonstration, it displays all the states along with their id that correspond to the countryId supplied by the AJAX request but the List is not returned to JSP.

The JavaScript code used to send this AJAX request alerts an error. It appears that there are some problems with JSON mapping. The same thing was working with the earlier version of the Spring framework (3.0.2). I'm not sure why does this cause problems with the higher version of Spring which is 3.2.0. Is there anything with the Spring version 3.2.0 which I might be missing?

If you needed to see the JavaScript code, the full JavaScript code to achieve this would be as follows - jQuery library 1.6

To be sure, the Jackson library is on the classpath and I'm not getting any error or exception on the server side. The AJAX request succeeds and it goes to the DAO via Spring and the list of type List<Object[]> is retrieved from the database but it is not a response of JSON to JSP (which could/should be mapped to a JavaScript array). presumably, it appears that there is something missing with JSON mapping which was however not the case with the earlier version of Spring.

I have tried to parse List<Object[]> in both of the frameworks, 3.0.2 and 3.2.0 such as

It works fine and the foreach loop iterates over the List of type List<Object[]>. So, the problem might be caused by the Spring framework itself. What else is required, I'm not sure. Why is it not mapped by Jackson? Is there any configuration setting that I may be missing?

Tiny
I think you got the http 406 message is owing to the requested url "/wagafashion/ajax/GetStateList.htm" is ended in with ".html".
From spring 3.2 media type first decided by uri extension.
Extension "html" means "text/html" , but your response is "application/json", so spring raise a HttpMediaTypeNotAcceptableException(I had meet this problem before).

but it again doesn't get JSON to work and the original problem still remains.

Failed to load resource: the server responded with a status of 406 (Not Acceptable)

I have the Jackson 1.9.8 (its downloadpage) library on the classpath. With Jackson 2.1.1, it doesn't work throwing the following exception at runtime.

java.lang.NoClassDefFoundError: org/codehaus/jackson/map/ObjectMapper

Because Jackson 2.1.1 has the class ObjectMapper in another package - com.fasterxml.jackson.databind.ObjectMapper

I'm now quite unsure as to how to make it run. I'm usually associated with StackOverflow. I have exactly the same question there but in addition to my own attempts, still I couldn't get JSON to work in Spring 3.2.0. Should I now think to use a lower version of Spring?

Comment

Tiny:
I think you should check whether dispatcher-servlet.xml naming space is correct or not.
According to 3.2 document, "content-negotiation-manager" is attribute of "mvc:annotation-driven".
And I don't think it is necessary to build a ContentNegotiatingViewResolver bean. Look at AnnotationDrivenBeanDefinitionParser's method getContentNegotiationManager(),
if "mvc:annotation-driven did not assigned a "content-negotiation-manager" then it build a default one.
So it need to assign a content-negotiation-manager as we expect.
I set favorPathExtension false to wish ContentNegotiationManager don't includ a PathExtensionContentNegotiationStrategy which determined mime type by file extension,
and set favorParameter false to say that I don't need a additional parameter "format" to determined mime-type;
and set ignoreAcceptHeader false to wish mine-type determined only by request header;
and mediaTypes property to map those available mine types.

Comment

Thanks for your timely response..I had org.codehaus.jackson.jackson-mapper and org.codehaus.jackson.jackson-core(1.9.8). Currently downloading the databind jar and trying whether it works..When I further debugged the code, there's more dimension to this. My return class CustomeResponse is invoked by a customised HTTP Message Converter.

The customised HTTP message converter was done to enforce UTF-8 charset even for Japanese language..
Earlier it was setting content type to "text/plain". Now I tried explicilty setting the content type to application/json in the message converter as shown above..But it still doesn't work.

Comment

dinup:
If you want to use jackson with spring default message converter to convert object to json.
Just include jackson-core and jackson-databind into classpath, Spring 32 will automatic include default message-converter,
Otherwise, config message-converters register-defaults with false and put your custom message converter into configuration.

Still the issue remain same..Am not sure whether its anything to do with the way my Custom Message Converter is handling the content type..Only those flows with Custom Converter is failing for now..Rest of the flow remains intact..I have pasted my custom converter code above..

I could see this in my logs all in these cases..This is happening after Controller sucessfully processses the request: