无法识别的字段...未标记为可忽略,EE8 / Jakarta EE
Unrecognized field ... not marked as ignorable, EE8 / Jakarta EE
我正在使用 Wildfly 18 和 Resteasy。
通过在我的 JSON API 的 body 上传递一个未知的 属性,我得到这个:
Unrecognized field "foo" (class xxx), not marked as ignorable
我知道这是 jackson 供应商的问题,在过去的项目中我用这个供应商解决过:
@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 设置忽略未知属性?
您可能默认使用 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,因此反序列化应该忽略未知属性而不是抛出异常。您可以按如下方式配置此 属性:
JsonbConfig config = new JsonbConfig().setProperty(org.eclipse.yasson.YassonConfig.FAIL_ON_UNKNOWN_PROPERTIES, true);
Jsonb jsonb = JsonbBuilder.create(config);
我正在使用 Wildfly 18 和 Resteasy。
通过在我的 JSON API 的 body 上传递一个未知的 属性,我得到这个:
Unrecognized field "foo" (class xxx), not marked as ignorable
我知道这是 jackson 供应商的问题,在过去的项目中我用这个供应商解决过:
@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 设置忽略未知属性?
您可能默认使用 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,因此反序列化应该忽略未知属性而不是抛出异常。您可以按如下方式配置此 属性:
JsonbConfig config = new JsonbConfig().setProperty(org.eclipse.yasson.YassonConfig.FAIL_ON_UNKNOWN_PROPERTIES, true);
Jsonb jsonb = JsonbBuilder.create(config);