Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

Springframework 3.1.2 adds support for automatic conversion to JSON via Jackson 2.x. There isn’t much that you need to do to make it work, with on caveat.

You need to be using at least Spring 3.1.2 to have Jackson 2.x support. Add the Jackson 2.x dependencies.


NOTE: If you only include the jackson-core artifact and do not include the jackson-databind artifact, your spring requests will return HTTP 406 errors and there will be no indication of what the real problem is.

What happens if you don’t include jackson-databind? The tag will not instantiate a MappingJackson2JsonView because it has an import for com.fasterxml.jackson.databind.ObjectMapper. There is no message because it just thinks that you are not using that.

Your spring mvc context should already have the .

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <context:component-scan base-package="com.appriss.jxp"/>

    <!-- Resolves view names for views returned by controllers -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"

Create a controller method. There are 2 things that must be done. First, specify that the method does produce json as the output. That will map against the “accept-header: application/json”. Then have the method return an object instead of a ModelAndView object. Annotate that object with @ResponseBody.

     * Returns a json list of first names.
     * @param term the beginning part of the first name
     * @return json string array of first names
    @RequestMapping(value = "/first", produces = "application/json")
    public @ResponseBody List<String> firstNames(@RequestParam String term) {
        return nameDao.getFirstNames(term);

There you have it. The rest is automatic.

July 15th, 2012

Posted In: Java, json, Spring


I had a Jquery form that I was generating with an Ajax call. The user selected a state, then I retrieved the licenses for the agency that they selected from the state. I also defaulted to the first agency and ran a query. The problem was when the state had no agencies, it still ran the query. I wanted to know if I actually had an agency selected. Here is the answer using Jquery:

if ($('#myElement').length > 0) {

July 3rd, 2012

Posted In: javascript, JQuery

Leave a Comment

WP to LinkedIn Auto Publish Powered By :