jQuery ui autocomplete not working in Spring 4.1


Codever Logo

(P) Codever is an open source bookmarks and snippets manager for developers & co. See our How To guides to help you get started. Public bookmarks repos on Github ⭐🙏


You may recall from my post Autocomplete search box with jQuery and Spring MVC, that I use jQuery ui autocomplete to dynamically search for keywords on Podcastpedia.org. I am now in the process of migrating the source base for Podcastpedia.org to Spring 4. I decided to go with the latest version 4.1.0.RELEASE and everything worked pretty smoothly until I got to test the auto-complete functionality presented in the post mentioned before.

Problem

What was actually the problem? Well, as the title says it, it won’t work… Nothing happens when I type something in the auto complete input box, and I am not getting getting any error message.

Debugging

The resource called the jQuery ajax getJSON:

$(document).ready(function() {
	//attach autocomplete
    $("#tagQuery").autocomplete({
    	minLength: 1,
    	delay: 500,

    	//define callback to format results
        source: function (request, response) {
            $.getJSON("/tags/get_tag_list", request, function(result) {
                response($.map(result, function(item) {
                    return {
                        // following property gets displayed in drop down
                        label: item.name + "(" + item.nrOfPodcasts + ")",
                        // following property gets entered in the textbox
                        value: item.name,
                        // following property is added for our own use
                        tag_url: "https://" + window.location.host + "/tags/" + item.tagId + "/" + item.name
                    }

                }));
        	});
    	},

    	//define select handler
    	select : function(event, ui) {
            if (ui.item) {
            	event.preventDefault();
                $("#selected_tags span").append('<a href=' + ui.item.tag_url + ' class="btn-metadata2" target="_blank">'+ ui.item.label +'</a>');
                //$("#tagQuery").value = $("#tagQuery").defaultValue
                var defValue = $("#tagQuery").prop('defaultValue');
                $("#tagQuery").val(defValue);
                $("#tagQuery").blur();
                return false;
            }
    	}

    });

});

seemed to have been “rightfully” mapped by Spring, when looking in the starting log:

4790 [main] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
- Mapped "{[/tags/get_tag_list],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}"
onto public java.util.List<org.podcastpedia.common.domain.Tag> org.podcastpedia.web.tags.TagController.getTagList(java.lang.String)

By using the Live HTTP Headers add-on from Firefox, I could establish that the GET call was sent from the JavaScript client’s side, but only to get a HTTP/1.1 406 Not Acceptable response:

http://localhost:8080/tags/get_tag_list?term=jav

GET /tags/get_tag_list?term=jav HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8080/tags/all/0
Cookie: __atuvc=0%7C22%2C0%7C23%2C19%7C24%2C17%7C25%2C3%7C26; __atsa=sh=facebook%2Ccompact%2Cgoogle_plusone_share%2Ctwitter; __utma=1.487167226.1396700237.1399960777.1400006838.6; __utmz=1.1396700237.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); preferredLanguage=en; JSESSIONID=qp5yyqw9486jq668t9atdjtd; jwplayer.volume=24
Connection: keep-alive
If-None-Match: "0a2e9f5514fc2deefce2de6323e3a54ec"

HTTP/1.1 406 Not Acceptable
Content-Type: text/html; charset=ISO-8859-1
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 311
Server: Jetty(9.1.5.v20140505)
----------------------------------------------------------

meaning that the Spring mapping cannot deliver the application/json format present in the accept header:

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

Solution

I remembered then, that when reading through the Migrating from earlier versions of the Spring Framework – Guide, the Jackson library was mentioned:

“Jackson 1.8.6 (note: minimum 2.1 as of Spring Framework 4.1, with Jackson 2.2.2 or later recommended)

On a second look, I realized that, although I didn’t use the minimum version 1.8.6:

<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-mapper-asl</artifactId>
	<version>1.9.12</version>
</dependency>
<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-jaxrs</artifactId>
	<version>1.9.12</version>
</dependency>
<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-core-asl</artifactId>
	<version>1.9.12</version>
</dependency>

, I had to upgrade the jackson libraries to a newer [2.1, *) version to work in Spring 4.1:

<dependency>
	<groupId>com.fasterxml.jackson.jaxrs</groupId>
	<artifactId>jackson-jaxrs-base</artifactId>
	<version>2.4.2</version>
</dependency>

This dependency will download also the jackson-core and jackson-databind artifacts for you.

Note: the groupId has changed for newer versions of the Jackson libraries.  Learn more in Upgrading Jackson 1.9 to 2.0

So, the short version of the blog post is upgrade your jackson libraries to minimum of 2.1 to work with Spring 4.1 🙂

Resources

GitHub

Web

Podcastpedia image

Adrian Matei

Creator of Podcastpedia.org and Codepedia.org, computer science engineer, husband, father, curious and passionate about science, computers, software, education, economics, social equity, philosophy - but these are just outside labels and not that important, deep inside we are all just consciousness, right?
Subscribe to our newsletter for more code resources and news

Adrian Matei (aka adixchen)

Adrian Matei (aka adixchen)
Life force expressing itself as a coding capable human being

routerLink with query params in Angular html template

routerLink with query params in Angular html template code snippet Continue reading