Getting known JSON properties from a class using Jackson

Jackson is a (or probably the most) popular framework for parsing JSON in Java. The Jackson API makes it easy to introspect an arbitrary class to get the available JSON properties:

// Construct a Jackson JavaType for your class
JavaType javaType = mapper.getTypeFactory().constructType(MyDto.class);

// Introspect the given type
BeanDescription beanDescription = mapper.getSerializationConfig().introspect(javaType);

// Find properties
List<BeanPropertyDefinition> properties = beanDescription.findProperties();

The BeanPropertyDefinition list should give you the details you need regarding the JSON properties.

The @JsonIgnoreProperties class level annotation is not taken into account with the above mentioned approach. But you can use an AnnotationIntrospector to get the properties ignored on class level:

// Get class level ignored properties
Set<String> ignoredProperties = mapper.getSerializationConfig().getAnnotationIntrospector()

Then filter properties removing the properties which are present in ignoredProperties:

// Filter properties removing the class level ignored ones
List<BeanPropertyDefinition> availableProperties =
        .filter(property -> !ignoredProperties.contains(property.getName()))

This approach works even if you have mix-ins defined for your class.

The AnnotationIntrospector#findPropertyIgnorals(Annotated) method was introduced in Jackson 2.8. The AnnotationIntrospector#findPropertiesToIgnore(Annotated, boolean) method can be used for older versions (but it’s deprecated since Jackson 2.8).