Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring ObjectMapper in Spring

my goal is to configure the objectMapper in the way that it only serialises element which are annotated with @JsonProperty.

In order to do so I followed this explanation which says how to configurate the objectmapper.

I included the custom objectmapper as described here.

However when the class NumbersOfNewEvents is serialized it still contains all attributes in the json.

Does anybody have a hint? Thanks in advance

Jackson 1.8.0 spring 3.0.5

CustomObjectMapper

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

servlet.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:mvc="http://www.springframework.org/schema/mvc"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">      <context:component-scan base-package="de.Company.backend.web" />      <mvc:annotation-driven />      <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="jacksonObjectMapper" />                 </bean>             </list>         </property>     </bean>      <bean id="jacksonObjectMapper" class="de.Company.backend.web.CompanyObjectMapper" /> </beans> 

NumbersOfNewEvents

public class NumbersOfNewEvents implements StatusAttribute {      public Integer newAccepts;     public Integer openRequests;      public NumbersOfNewEvents() {         super();     } } 
like image 379
Steve Eastwood Avatar asked Oct 21 '11 18:10

Steve Eastwood


People also ask

What is the use of ObjectMapper in spring boot?

Overview. When using JSON format, Spring Boot will use an ObjectMapper instance to serialize responses and deserialize requests. In this tutorial, we'll take a look at the most common ways to configure the serialization and deserialization options. To learn more about Jackson, be sure to check out our Jackson tutorial.

Can we Autowire ObjectMapper spring boot?

If you're working with Spring Boot, there's a section in the manual dedicated to working with the ObjectMapper If you create a default Jackson2ObjectMapperBuilder @Bean , you should be able to autowire that same ObjectMapper instance in your controller.

Should you inject ObjectMapper?

Yes, that is safe and recommended.

What is use of ObjectMapper in Java?

ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model ( JsonNode ), as well as related functionality for performing conversions.


2 Answers

Using Spring Boot (1.2.4) and Jackson (2.4.6) the following annotation based configuration worked for me.

@Configuration public class JacksonConfiguration {      @Bean     public ObjectMapper objectMapper() {         ObjectMapper mapper = new ObjectMapper();         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);         mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);          return mapper;     } } 
like image 59
Alex Avatar answered Sep 20 '22 13:09

Alex


It may be because I'm using Spring 3.1 (instead of Spring 3.0.5 as your question specified), but Steve Eastwood's answer didn't work for me. This solution works for Spring 3.1:

In your spring xml context:

<mvc:annotation-driven>     <mvc:message-converters>         <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>         <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>         <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">             <property name="objectMapper" ref="jacksonObjectMapper" />         </bean>             </mvc:message-converters> </mvc:annotation-driven>  <bean id="jacksonObjectMapper" class="de.Company.backend.web.CompanyObjectMapper" /> 
like image 31
Andrew Newdigate Avatar answered Sep 18 '22 13:09

Andrew Newdigate