pierrefevrier
pierrefevrier

Reputation: 1640

Can't deal with JAXB anotation using Jackson

I'm trying to build a JSON webservice consumer by using restTemplate (Spring) and Jackson 2.3.0

The problem is the binding of pojo like this one:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DerniereFactureTest", propOrder = { "montantTTC" })
public class DerniereFactureTest {
  @XmlElement(name = "montant_TTC", required = true)
  protected BigDecimal montantTTC;
  ...<Getter and setter>...
}

Jackson output this error:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "montant_TTC" (class com.bouygtel.vgc.transverse.composant.ws.DerniereFactureTest), not marked as ignorable (one known property: "montantTTC"]) (......) Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "montant_TTC" (class com.bouygtel.vgc.transverse.composant.ws.DerniereFactureTest), not marked as ignorable (one known property: "montantTTC"])

I precise I can't change Pojo annotations.

Here is my Spring configuration:

<!-- Client REST -->
<bean class="org.springframework.web.client.RestTemplate" />

<!-- JAX-B: mapper JSON<->DTO -->
<bean
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="objectMapper" ref="jacksonObjectMapper" />
</bean>

<bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
    <property name="annotationIntrospector" ref="jaxbAnnotationInspector" />
</bean>

<bean id="jaxbAnnotationInspector"
    class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector" />

When debugging it looks to me JaxbAnnotationIntrospector is not used (but JacksonAnnotationIntrospector instead).

Any idee of the way to make Jackson use JaxB annotations ?

Thanks by advice.

Upvotes: 0

Views: 1844

Answers (1)

pierrefevrier
pierrefevrier

Reputation: 1640

I finally solve myself the problem with this Spring conf:

    <!-- le client REST -->
<bean class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes" value="application/json" />
                <property name="objectMapper">
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                        <property name="annotationIntrospector">
                            <bean
                                class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector" />
                        </property>
                    </bean>
                </property>
            </bean>
        </list>
    </property>
</bean>

Upvotes: 1

Related Questions