Java 14 records json serialization

Viewed 6495

Currently experimenting with the Records implements from java 14, everything looks nice but since the accessors are slightly different and jackson is not being able to deserialize and giving the following error:

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.x.x.x.xTracking and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

I checked all around the internet, including jackson and gson github to check the jep 359 support but havent found a single comment. Am i missing something really straight forward?

Yes i am aware that the java 14 is still not released and that records is only in preview in this version but would expect some comments at least.

5 Answers

For someone else experimenting, i went around, not proudly, with the following:

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer(){
        return builder ->
                builder.visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    }

With "pure" Json-B you can do it this way:

public class RecordPropertyVisibilityStrategy implements PropertyVisibilityStrategy {
   @Override
   public boolean isVisible(Field field) {return true;}

   @Override
   public boolean isVisible(Method method) {return false;}
}

Then

@JsonbVisibility(RecordPropertyVisibilityStrategy.class)
public record MyRecord(Long id, String attr) {}

I did Jackson dealing with this problem in Spring Boot 2.4.1 application annotating the record with

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)

Alternatively, for Spring Boot 2.4.5 and Java 16, it may be enabled via application.properties, like so:

spring.jackson.visibility.field=any

See more on the Spring Boot documentation.

Related