jackson objectMapper Error “No serializer found for class”

No serializer found for class and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS) )

The above error in Java is encountered when we use static field with objectMapper like

private static String name;
HelloWorld helloClasscar = new HelloWorld();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue( new FileOutputStream(“src/output.json”), helloClasscar);

Jackson supports only those fields that are either public, or have a public getter methods. Serializing an entity that has all fields private or package private will fail:

Solution: Options to resolve this issue:

1) Either create getter for the fields:
private String name;

2) Jackson provide below to allow the private and package private fields to be detected without getters, and serialization will work correctly

private String name;
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

More details: https://www.baeldung.com/jackson

Leave a Reply

Your email address will not be published. Required fields are marked *