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
Release: 2024-12-04 06:16:11
Original
979 people have browsed it

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));
    }
}
Copy after login

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

<bean>
Copy after login

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>
Copy after login

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

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

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

@JsonProperty
public Integer newAccepts;
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template