I am using Spring MVC with JSON as specified in Ajax Simplification Spring 3.0 article.
After so many attempts and variations of my code depending on advice found on various forums, my code still doesn't work.
I keep on getting the following error: (406) The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
I have in my appconfig.xml as required.
app-config.xml
<context:component-scan base-package="org.ajaxjavadojo" />
<!-- Configures Spring MVC -->
<import resource="mvc-config.xml" />
mvc-config.xml
<mvc:annotation-driven />
<!-- Forwards requests to the "/" resource to the "index" view -->
<mvc:view-controller path="/" view-name="index"/>
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
This is what I have for my controller
@Controller
@RequestMapping (value = "/convert")
public class ConversionController {
@RequestMapping(method=RequestMethod.GET)
public String getConversionForm(){
return "convertView";
}
@RequestMapping(value = "/working", headers="Accept=application/json", method=RequestMethod.GET)
public @ResponseBody Conversion getConversion(){
Conversion d = new Conversion("d");
return d;
}
}
jsp jquery call
function convertToDecimal(){
$.getJSON("convert/working", {key: "r"}, function(aConversion){
alert("it worked.");
$('#decimal').val(aConversion.input);
});
}
I would really appreciate any input on this issue. Thank you
I also faced this same issue and I downloaded this [jar]: (http://www.java2s.com/Code/Jar/j/Downloadjacksonall190jar.htm)! and placed in lib folder and the app works like a charm :)
I have used java configuration and i got this same error. I have missed to add the @EnableWebMvc to the configuration file. This error is resolved after i add the @EnableWebMvc in my webconfig file.
Also the Object that is returned from your Spring Controller, should have proper getter and setter methods.
AppInitializer.java
Also make sure the Object that is returned, has the proper getter and setter.
Example:
TaskInfo object should have proper getter and setter. if not, 406 error will be thrown.
POM File for Reference:
To return JSON response from
@ResponseBody
-annotated method, you need two things:<mvc:annotation-driven />
(you already have it)You don't need
ContentNegotiatingViewResolver
andheaders
in@RequestMapping
.Using jQuery , you can set contentType to desired one (application/json; charset=UTF-8' here) and set same header at server side.
REMEMBER TO CLEAR CACHE WHILE TESTING.
issue is not related to jquery . even bug is saying it is server side issue . please make sure that following 2 jar present in class path :-
jackson-core-asl-1.9.X.jar jackson-mapper-asl-1.9.X.jar
I had this problem after I upgraded Spring to 4.1.x from 3.2.x. I fixed by upgrading Jackson from 1.9.x to 2.2.x (fasterxml)