I'm using Wildfly 18 with Resteasy.
By passing an unknown property on body of my JSON API, I get this:
Unrecognized field "foo" (class xxx), not marked as ignorable
I know this is a jackson provider issue, on past projects I solved with this provider:
@Provider
public class JerseyObjectMapperProvider implements ContextResolver<ObjectMapper> {
private ObjectMapper defaultMapper;
public JerseyObjectMapperProvider() {
MapperConfigurator mapperConfig = new MapperConfigurator(null, JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS);
mapperConfig.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
defaultMapper = mapperConfig.getDefaultMapper();
}
@Override
public ObjectMapper getContext(Class<?> type) {
return defaultMapper;
}
}
Now I've moved on EE8 / JakartaEE which contains JsonB directives on framework, therefore I'm using JsonbConfig. This is my provider:
@Provider
@Priority(Priorities.ENTITY_CODER)
public class JSONBConfiguration implements ContextResolver<Jsonb> {
private Jsonb jsonb;
public JSONBConfiguration() {
JsonbConfig config = new JsonbConfig()
.withFormatting(true)
.withNullValues(true)
.withPropertyNamingStrategy(PropertyNamingStrategy.IDENTITY)
.withPropertyOrderStrategy(PropertyOrderStrategy.ANY)
.withDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ITALY);
jsonb = JsonbBuilder.create(config);
}
@Override
public Jsonb getContext(Class<?> type) {
return jsonb;
}
}
Is there any way to set ignore unknown properties with JsonbConfig like the mapper above ?
You're probably using Yasson, the reference implementation of JSON-B. Yasson has an option FAIL_ON_UNKNOWN_PROPERTIES. This option is non-standard, i.e. it is not part of the JSON-B specification. This option is disabled by default, so deserialization should ignore unknown properties instead of throwing an exception. You can configure this property as follows:
JsonbConfig config = new JsonbConfig().setProperty(org.eclipse.yasson.YassonConfig.FAIL_ON_UNKNOWN_PROPERTIES, true);
Jsonb jsonb = JsonbBuilder.create(config);