2

I have two ObjectMapper instances of Jackson. (Using v2.8.3)

The first one is instantiated as follows:

ObjectMapper objectMapper = new ObjectMapper();

The other one is autowired from the Spring Boot context via @Autowired.

Both have the same visibility configurations like this:

enter image description here

But they produce different serializations for the same object. The differences I realized are as follows:

  • Order of the serialized fields
  • Serialization of protected transient fields. (The autowired instance does not serialize them at all.)
  • Case differences in the serialized fields i.e. the former generates "monitoringUserID" while the latter "MonitoringUserID".

What I want is that the autowired mapper would also serialize the protected transient fields.

I'd also be glad if you tell me the reasons on the other differences in both serializations.

A simplified version of an example class being serialized:

public class ClauseType implements Serializable {
    protected transient List<ClauseTypeNoteItem> noteItems;

    public ClauseType() {
    }

    public List<ClauseTypeNoteItem> getNoteItems() {
        ...
    }

    public void setNoteItems(List<ClauseTypeNoteItem> value) {
        ...
    }
}

Complete ClauseType class: https://pastebin.com/m3h1hesn

Complete ClauseTypeNoteItem class: https://pastebin.com/dmphNV4e

Edit: I realized that both instances had difference mapper features. According to the docs, after enabling the DEFAULT_VIEW_INCLUSION featured of the autowired instance, they had the same mapper features represented as 1068991. However the differences are still there. I also realized that the autowired mapper has two registered modules: org.springframework.boot.jackson.JsonComponentModule and com.fasterxml.jackson.datatype.joda.JodaModule. I'm not sure whether the second module has an effect on the results I'm getting.

Thanks in advance.

Community
  • 1
  • 1
suat
  • 4,239
  • 3
  • 28
  • 51
  • can you please show class you are serializing. Because for me both of them serialize only public fields. – Ruslan Akhundov May 02 '18 at 16:11
  • @RuslanAkhundov just updated the post – suat May 02 '18 at 16:26
  • Spring Boot [docs](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-customize-the-jackson-objectmapper) make it clear that the default Jackson mapper provided by Spring has some customized properties out of the box. Can you narrow it down to those differences? – JellyRaptor May 02 '18 at 16:28
  • Yes, the DEFAULT_VIEW_INCLUSION feature was different in both settings. But I still have the different results even after enabling it. Please see my edit. – suat May 02 '18 at 17:06

2 Answers2

2

It turned out that Spring injects a JaxbAnnotationIntrospector coming from jackson-module-jaxb-annotations maven dependency. During the instantiations of BeanDescriptions, the Jaxb introspector overrides the visibility checker settings that I provided. So, as a solution I will inject a new ObjectMapper bean and mark it as @Primary.

If you think of any other better solution, let me know

suat
  • 4,239
  • 3
  • 28
  • 51
0

In Spring Boot the Jackson ObjectMapper is build and customised by Jackson2ObjectMapperBuilder object. The main source of configuration are the spring.jackson.* properties as explained in the docs:

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111