DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Fun Is the Glue That Makes Everything Stick, Also the OCP
  • Hibernate Get vs. Load
  • Measuring Service Performance: The Whys and Hows
  • System Testing and Best Practices
  1. DZone
  2. Coding
  3. Frameworks
  4. jQuery UI Autocomplete Not Working in Spring 4.1

jQuery UI Autocomplete Not Working in Spring 4.1

Adrian Matei user avatar by
Adrian Matei
·
Sep. 29, 14 · Interview
Like (0)
Save
Tweet
Share
4.50K Views

Join the DZone community and get the full member experience.

Join For Free

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: "http://" + 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=[]}"

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 :)

Spring Framework JQuery UI

Published at DZone with permission of Adrian Matei, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Fun Is the Glue That Makes Everything Stick, Also the OCP
  • Hibernate Get vs. Load
  • Measuring Service Performance: The Whys and Hows
  • System Testing and Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: