Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Spring 3 + JSON : HTTP status 406?

I'm trying to get a list of Cities by sending the State name through Ajax in my SpringMVC 3.0 project. For the purpose, I've used the following call (using jQuery) in my JSP:

    <script type="text/javascript">
function getCities() {
    jq(function() {
        jq.post("getCities.html",
                    {   stateSelect:  jq("#stateSelect").val()},
                        function(data){
                            jq("#cities").replaceWith('<span id="cities">Testing</span>');
                    });
    });
}
</script>

And here's my Controller code:

@RequestMapping(value = "/getCities", method = RequestMethod.POST)
    public @ResponseBody List<StateNames> getCities(@RequestParam(value="stateSelect", required=true) String stateName,
                                Model model) {
        // Delegate to service to do the actual adding
        List<StateNames> listStates = myService.listCityNames(stateName);

        // @ResponseBody will automatically convert the returned value into JSON format
        // You must have Jackson in your classpath
        return listStates;
    }

But I get HTTP 406 error stating the following when i run it: 406 Not Acceptable The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.

I've used Jackson in my Maven dependencies & have defined in my context file. I've googled extensively & I guess the problem is @ResponseBody is not automatically converting my List to appropriate JSON object.

My Firebug says:

Response Headers  
Server  Apache-Coyote/1.1  
Content-Type    text/html;charset=utf-8  
Content-Length  1070  
Date    Sat, 12 Feb 2011 13:09:44 GMT  

Request Headers  
Host    localhost:8080  
User-Agent  Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13  
Accept  */*  
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  
Keep-Alive  115  
Connection  keep-alive  
Content-Type    application/x-www-form-urlencoded; charset=UTF-8  
X-Requested-With    XMLHttpRequest  
Referer http://localhost:8080/MyApplication/  
Content-Length  17  
Cookie  JSESSIONID=640868A479C40792F8AB3DE118AF12E0  
Pragma  no-cache  
Cache-Control   no-cache 

Please guide me. What am i doing wrong?? HELP!!

like image 526
Tonmoy Goswami Avatar asked Feb 13 '11 07:02

Tonmoy Goswami


People also ask

How do I fix HTTP 406 not acceptable?

The primary way to address and fix a 406 error is by checking the source code for issues in the Accept-, Request-, and Response- headers. The easiest way to review Accept- and Response- headers is to open a webpage in your browser, right-click, and select Inspect.

What is Error 406 in Project Makeover?

A 406 Error – Not Acceptable will return if the file format is not supported by the server.


3 Answers

As Peter had written in his comment, the cause of the problem is inability of Spring to load Jackson. It is not loaded by dependencies by default. After I've added the dependency

  <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-jaxrs</artifactId>
    <version>1.9.2</version>
  </dependency>

the JSON was returned after typing the address in the browser, without any tricks with Accept headers (as it is supposed to do).

Tested on Tomcat 7.0.

like image 126
Danubian Sailor Avatar answered Oct 11 '22 22:10

Danubian Sailor


You have incorrect response content type it supposed to be application/json.

You need to add jackson to your /lib directory.

and you should have

<mvc:annotation-driven /> 

In your serlvet-name.xml file.

In addition I recommend you to map your request as get and try to browse it with Google Chrome,to see if it returns correct result. It has very good json representation.

like image 41
danny.lesnik Avatar answered Oct 11 '22 22:10

danny.lesnik


The problem is not on server side, but on the client one.

Take a look at the error message carefully: The requested resource (generated by server side) is only capable of generating content (JSON) not acceptable (by the client!) according to the Accept headers sent in the request.

Examine your request headers:

Accept  */*

Try this way:

function getCities() {
    jq(function() {
        jq.post(
            "getCities.html",                          // URL to post to
            { stateSelect: jq("#stateSelect").val() }, // Your data
            function(data) {                           // Success callback
                jq("#cities").replaceWith('<span id="cities">Testing</span>');
            },
            "json"                                     // Data type you are expecting from server
        );
    });
}

This will change your Accept header to the following (as of jQuery 1.5):

Accept: application/json, text/javascript, */*; q=0.01

This will explicitly tell the server side that you are expecting JSON.

like image 35
Ihor Kaharlichenko Avatar answered Oct 11 '22 21:10

Ihor Kaharlichenko