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.

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

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="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc.xsd">

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

    <!-- Resolves view names for views returned by controllers -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:viewClass="org.springframework.web.servlet.view.JstlView"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp"/>
</beans>

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

14 Comments

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

LinkedIn Auto Publish Powered By : XYZScripts.com