Toseef Zafar
Toseef Zafar

Reputation: 1787

@ResponseBody not working with spring 4.2.4 - not a duplicate I have checked all others

Here is my setup:

spring version: 4.2.4.RELEASE

Jackson libs:

  <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.3</version>
    </dependency>

spring config:

<mvc:annotation-driven />

Request Object:

public class TagSearchCriteria {
    private String term;

    public void setTerm(String term){
        this.term = term;
    }

    public String getTerm(){
        return this.term;
    }
}

Response Object

public class TagSearchResponse {
    private String result;

    public void setResult(String result){
        this.result = result;
    }

    public String getResult(){
        return this.result;
    }
}

And here is the controller method:

@RequestMapping(value = "/tagvalue.htm", method = RequestMethod.POST)
    public @ResponseBody TagSearchResponse getTags(@RequestBody TagSearchCriteria tagSearchCriteria) {

        Tag tag = tagDao.getTags(tagSearchCriteria.getTerm());
        TagSearchResponse tagSearchResponse = new TagSearchResponse();
        tagSearchResponse.setResult(tag.getTagName());
        return tagSearchResponse;
    }

And finally here is my AJAX call

$("#tag").keyup(function() {
        var tagValue = $("#tag").val();
        if (tagValue.length > 2) {
            var data = {}
            data["term"] = $("#tag").val();
            $.ajax({
                type : "POST",
                contentType : "application/json",
                url : "tagvalue.htm",
                data : JSON.stringify(data),
                dataType : 'json',
                timeout : 100000,
                success : function(data) {
                    console.log("SUCCESS: ", data);
                    display(data);
                },
                error : function(e) {
                    console.log("ERROR: ", e);
                    display(e);
                },
                done : function(e) {
                    console.log("DONE");
                }
            });
        }
    });

what I get in response is 406 error

HTTP Status 406 - Status report The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

in controller, upto return tagSearchResponse; its all good and this return object is a valid object being returned

Upvotes: 0

Views: 1440

Answers (1)

JB Nizet
JB Nizet

Reputation: 692161

The problem is with your mapping. By default, Spring uses the extension in the URL to decide what type of content to return (html, json, xml, etc.).

Your method is mapped to /tagvalue.htm, but you want it to return json (which is quite confusing, BTW).

Change the mapping to /tagvalue, and use /tagvalue or /tagvalue.json to reach the endpoint.

More information about this mechanism is available in the documentation.

Upvotes: 1

Related Questions