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();
}
}
I found the solution now based on https://github.com/FasterXML/jackson-module-hibernate
I extended the object mapper and added the attributes in the inherited constructor.
Then the new object mapper is registered as a bean.
There is
org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean
for a long time. Starting from 1.2 release of Spring Boot there isorg.springframework.http.converter.json.Jackson2ObjectMapperBuilder
for Java Config.In String Boot configuration can be as simple as:
in
classpath:application.properties
or some Java code in@Configuration
class:See:
I've used this with Jackson 2.x and Spring 3.1.2+
servlet-context.xml:
Note that the root element is
<beans:beans>
, so you may need to removebeans
and addmvc
to some of these elements depending on your setup.au.edu.unimelb.atcom.transfer.json.mappers.JSONMapper.java:
DateSerializer.java:
DateDeserializer.java:
Using Spring Boot (1.2.4) and Jackson (2.4.6) the following annotation based configuration worked for me.
SOLUTION 1
First working solution (tested) useful especially when using @EnableWebMvc:
SOLUTION 2
Of course the common approach below works too (also working with @EnableWebMvc):
Why @EnableWebMvc usage is a problem?
@EnableWebMvc is using
DelegatingWebMvcConfiguration
which extendsWebMvcConfigurationSupport
which does this:which means that there's no way of injecting your own
ObjectMapper
with the purpose of preparing it to be used for creating the defaultMappingJackson2HttpMessageConverter
when using @EnableWebMvc.Above Spring 4, there is no need to configure
MappingJacksonHttpMessageConverter
if you only intend to configureObjectMapper
.(configure
MappingJacksonHttpMessageConverter
will cause you to lose other MessageConverter)You just need to do:
And in your Spring configuration, create this bean: