Home >Java >javaTutorial >How to Customize Spring's ObjectMapper to Serialize Only @JsonProperty Annotated Properties?

How to Customize Spring's ObjectMapper to Serialize Only @JsonProperty Annotated Properties?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-04 06:16:11979browse

How to Customize Spring's ObjectMapper to Serialize Only @JsonProperty Annotated Properties?

Configuring ObjectMapper in Spring

In Spring applications, the ObjectMapper is a crucial component for serializing and deserializing JSON data. You can customize the ObjectMapper to meet specific requirements, such as only serializing properties annotated with @JsonProperty.

To achieve this, the first step is to create a custom ObjectMapper class that extends the base ObjectMapper class provided by Jackson. Override the default visibility checker to exclude non-annotated properties:

public class MyCustomObjectMapper extends ObjectMapper {
    public MyCustomObjectMapper() {
        super();
        setVisibilityChecker(getSerializationConfig()
                .getDefaultVisibilityChecker()
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
                .withFieldVisibility(JsonAutoDetect.Visibility.NONE)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.DEFAULT));
    }
}

Next, register the custom ObjectMapper bean in the Spring configuration file (servlet.xml):

<bean>

Finally, configure the annotation-driven MVC framework to use the custom ObjectMapper:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <property name="objectMapper" ref="customObjectMapper" />
            </bean>
        </list>
    </property>
</bean>

In the example code provided, the NumbersOfNewEvents class contains two public attributes:

public class NumbersOfNewEvents implements StatusAttribute {
    public Integer newAccepts;
    public Integer openRequests;
    // ...
}

However, only the newAccepts attribute is annotated with @JsonProperty:

@JsonProperty
public Integer newAccepts;

By configuring the ObjectMapper as described above, only the newAccepts attribute should be serialized when the NumbersOfNewEvents object is converted to JSON. This is because the custom ObjectMapper will exclude non-annotated properties during serialization.

The above is the detailed content of How to Customize Spring's ObjectMapper to Serialize Only @JsonProperty Annotated Properties?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn