Arthur
Arthur

Reputation: 1176

JsonIgnore does not work with Spring

How fix this?

I tried to add @JsonIgnore to getter, but result was same.

POJO:

public class Category {
    // Omitted details

    @JsonIgnore
    private List<Category> children;
}

From build.gradle:

'com.fasterxml.jackson.core:jackson-annotations:2.7.3'

Spring version:

4.2.6.RELEASE

Context:

<bean id="jacksonMessageConverter"
      class="org.springframework.http.converter.json.GsonHttpMessageConverter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter"/>
        </list>
    </property>
</bean>

Upvotes: 0

Views: 1788

Answers (2)

Ali Dehghani
Ali Dehghani

Reputation: 48193

First off, add jackson-databind dependency:

'com.fasterxml.jackson.core:jackson-databind:2.7.3'

You can remove the jackson-annotations, since it will be resolved transitively. Then, with approperaite Jackson jar on the classpath, Spring MVC will automatically configure the required HttpMessageConverters for you. So, there is no need to register them manually. Hence, you can safely get rid of the following:

<bean id="jacksonMessageConverter"
      class="org.springframework.http.converter.json.GsonHttpMessageConverter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter"/>
        </list>
    </property>
</bean>

Spring usually uses well defined and long names for its abstractions, so you can easily find out what is the purpose of each one. Obviously GsonHttpMessageConverter is a HttpMessageConverter for Gson, not Jackson. Checkout Spring documentation for more detailed discussion.

Upvotes: 1

Jaiwo99
Jaiwo99

Reputation: 10017

Please have a look at your code:

<bean id="jacksonMessageConverter"
      class="org.springframework.http.converter.json.GsonHttpMessageConverter"/>

You need the Jackson Version of the converter

Upvotes: 1

Related Questions