A month ago, I asked this question. I implemented the selected solution using the following method to register it with Spring Boot (2.7):
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> builder.annotationIntrospector(new EmptyAsNullCollectionJacksonAnnotationIntrospector());
The result is that nothing is getting serialized properly. It is just blank with maybe an empty array "[]." I've seen this happen when the JsonView is wrong but I don't know how this code could have caused that.
The other problem is that while most things are deserialized properly, the one thing that doesn't work is collections which have members. Instead of those members being deserialized back into their Java objects, they're deserialized as LinkedHashMaps.
Perhaps I'm going about this in the wrong way. Here are the two problems I'm trying to solve:
My front end guys would like to get empty collections instead of nulls. I could fix this problem by just initializing every collection. Easily done but I was hoping for an automated solution so I wouldn't have to remember to do it.
We implemented Envers auditing on several of our tables and turned on modifiedFlags for a couple of them. It seems that when Hibernate gets an empty collection where it originally had a null, it flips the modified flag to true. This then shows up on modification reports as a change where none actually took place. The solution seems to be to ensure that empty collections are converted to nulls before being persisted.
The original question was me trying to solve both these problems in one fell swoop. If there is a better solution to these problems, even individually, I'd like to hear it. If my original idea was the best solution, I need some help getting it to work.
Thanks.