2
votes

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.

2
can you please show class you are serializing. Because for me both of them serialize only public fields. - Ruslan Akhundov
@RuslanAkhundov just updated the post - suat
Spring Boot docs 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
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

2 Answers

2
votes

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

0
votes

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: