对于 jackson 1.9x 或更低版本,您可以使用对象映射器提供程序忽略未知属性
@Provider
@Component
public class JerseyObjectMapperProvider implements ContextResolver<ObjectMapper> {
@Override
public ObjectMapper getContext(Class<?> type) {
ObjectMapper result = new ObjectMapper();
result.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return result;
}
}
对于 jackson 2.x 及更高版本,您可以使用对象映射器提供程序忽略未知属性
@Provider
@Component
public class JerseyObjectMapperProvider implements ContextResolver<ObjectMapper> {
@Override
public ObjectMapper getContext(Class<?> type) {
ObjectMapper result = new ObjectMapper();
result.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return result;
}
}
Spring 不会自动发现 Jersey 类。必须手动注册它们。
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(JerseyObjectMapperProvider.class);
}
}