this is my javascript:
function getWeather() {
$.getJSON('getTemperature/' + $('.data option:selected').val(), null, function(data) {
alert('Success');
});
}
this is my controller:
@RequestMapping(value="/getTemperature/{id}", headers="Accept=*/*", method = RequestMethod.GET)
@ResponseBody
public Weather getTemparature(@PathVariable("id") Integer id){
Weather weather = weatherService.getCurrentWeather(id);
return weather;
}
spring-servlet.xml
<context:annotation-config />
<tx:annotation-driven />
Getting this error:
GET http://localhost:8080/web/getTemperature/2 406 (Not Acceptable)
Headers:
Response Headers
Server Apache-Coyote/1.1
Content-Type text/html;charset=utf-8
Content-Length 1070
Date Sun, 18 Sep 2011 17:00:35 GMT
Request Headers
Host localhost:8080
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
X-Requested-With XMLHttpRequest
Referer http://localhost:8080/web/weather
Cookie JSESSIONID=7D27FAC18050ED84B58DAFB0A51CB7E4
Interesting note:
I get 406 error, but the hibernate query works meanwhile. This is what tomcat log says, everytime when I change selection in dropbox:
select weather0_.ID as ID0_0_, weather0_.CITY_ID as CITY2_0_0_, weather0_.DATE as DATE0_0_, weather0_.TEMP as TEMP0_0_ from WEATHER weather0_ where weather0_.ID=?
What could the problem be? There were two similar questions in SO before, I tried all the accepted hints there, but they did not work I guess...
Any suggestions? Feel free to ask questions...
As @atott mentioned.
If you have added the latest version of Jackson in your pom.xml, and with Spring 4.0 or newer, using
@ResponseBody
on your action method and@RequestMapping
configured withproduces="application/json;charset=utf-8"
, however, you still got 406(Not Acceptable), I guess you need to try this in your MVC DispatcherServlet context configuration:That's the way how I resolved my issue finally.
I had the same problem unfortunately non of the solution here solved my problem as my problem was something in a different class.
I first checked that all dependencies are in place as suggested by @bekur then I checked the request/response that travels from clients to the server all headers was in place an properly set by Jquery. I then checked the
RequestMappingHandlerAdapter
MessageConverters
and all 7 of them were in place, I really started to hate Spring ! I then updated to from Spring4.0.6.RELEASE
to4.2.0.RELEASE
I have got another response rather than the above. It wasRequest processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type
Here is my controller method
The solution was to make UploadPictureResult not to extend WebResponse
For some reason spring was not able to determine the how to convert UploadPictureReslt when it extended WebResponse
I was having the same problem because I was missing the @EnableMvc annotation. (All of my spring configuration is annotation based, the XML equivalent would be mvc:annotation-driven)
I had same issue, with Latest Spring 4.1.1 onwards you need to add following jars to pom.xml.
also make sure you have following jar:
406 Spring MVC Json, not acceptable according to the request "accept" headers
Make sure that following 2
jar
's are present in class path.If any one or both are missing then this error will come.