我将 Wildfly 18 与 Resteasy 一起使用。
通过在我的 JSON API 的主体上传递一个未知属性,我得到了这个:
无法识别的字段“foo”(xxx 类),未标记为可忽略
我知道这是杰克逊提供者的问题,在过去我用这个提供者解决的项目中:
@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;
}
}
现在我已经转移到 EE8 / JakartaEE 上,其中包含框架上的 JsonB 指令,因此我正在使用JsonbConfig。这是我的提供者:
@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;
}
}
有没有办法像上面的映射器一样使用JsonbConfig设置忽略未知属性?