Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was trying to <code>@Autowire</code> a Spring-managed service into my <code>Deserializer</code>. Somebody tipped me off to Jackson using the <code>new</code> operator when invoking the serializers/deserializers. This meant no auto-wiring of Jackson's instance of my <code>Deserializer</code>. Here's how I was able to <code>@Autowire</code> my service class into my <code>Deserializer</code>:</p> <p><strong>context.xml</strong></p> <pre><code>&lt;mvc:annotation-driven&gt; &lt;mvc:message-converters&gt; &lt;bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt; &lt;property name="objectMapper" ref="objectMapper" /&gt; &lt;/bean&gt; &lt;/mvc:message-converters&gt; &lt;/mvc&gt; &lt;bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"&gt; &lt;!-- Add deserializers that require autowiring --&gt; &lt;property name="deserializersByType"&gt; &lt;map key-type="java.lang.Class"&gt; &lt;entry key="com.acme.Anchor"&gt; &lt;bean class="com.acme.AnchorDeserializer" /&gt; &lt;/entry&gt; &lt;/map&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>Now that my <code>Deserializer</code> is a Spring-managed bean, auto-wiring works!</p> <p><strong>AnchorDeserializer.java</strong></p> <pre><code>public class AnchorDeserializer extends JsonDeserializer&lt;Anchor&gt; { @Autowired private AnchorService anchorService; public Anchor deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { // Do stuff } } </code></pre> <p><strong>AnchorService.java</strong></p> <pre><code>@Service public class AnchorService {} </code></pre> <hr> <p><strong>Update</strong>: While my original answer worked for me back when I wrote this, @xi.lin's response is exactly what is needed. Nice find!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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