Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't say how you're using Jackson in Spring, so I'll assume you're using it through <code>&lt;mvc:annotation-driven/&gt;</code> and the <code>@RequestBody</code> and/or <code>@ResponseBody</code> annotations.</p> <p>One of the things that <code>&lt;mvc:annotation-driven/&gt;</code> does is to register a <code>AnnotationMethodHandlerAdapter</code> bean which comes with a number of pre-configured <code>HttpMessageConverter</code> beans, including <code>MappingJacksonHttpMessageConverter</code>, which handles marshalling to and from Jackson-annotated model classes.</p> <p>Now <code>MappingJacksonHttpMessageConverter</code> has a <code>setObjectMapper()</code> method, which allows you to override the default <code>ObjectMapper</code>. But since <code>MappingJacksonHttpMessageConverter</code> is created behind the scenes by <code>&lt;mvc:annotation-driven/&gt;</code>, you can't get to it.</p> <p>However, <code>&lt;mvc:annotation-driven/&gt;</code> is just a convenient short-cut. It's just as a valid to declare your own <code>AnnotationMethodHandlerAdapter</code> bean, injecting into it your own <code>MappingJacksonHttpMessageConverter</code> bean (via the <code>messageConverters</code> property), and injecting your own customized <code>ObjectMapper</code> into that.</p> <p>You then have the problem of how to build a custom <code>ObjectMapper</code>, since it's not a very Spring-friendly class. I suggest writing your <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-extension-factorybean" rel="noreferrer">own simple implementation of <code>FactoryBean</code></a>.</p> <p>So you'd end up with something like this:</p> <pre><code>&lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt; &lt;property name="messageConverters"&gt; &lt;bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt; &lt;property name="objectMapper"&gt; &lt;bean class="com.x.MyObjectMapperFactoryBean"/&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload