无法通过或将a直接添加
DeserializationProblemHandler到。调用构建器是不行的,因为结果对于该方法而言是本地的:Spring本身将稍后调用,从而创建另一个实例。
ObjectMapper``Jackson2ObjectMapperBuilder``Jackson2ObjectMapperBuilderCustomizer``build()``ObjectMapper``build()``ObjectMapper
但是,可以通过注册Jackson模块来做到这一点:
- 建造者有一种
modules()
方法 - 该模块可通过实例访问
setupModule()
,该SetupContext
实例具有一个addDeserializationProblemHandler()
方法
然后这应该工作
@Beanpublic Jackson2ObjectMapperBuilderCustomizer customizer() { return new Jackson2ObjectMapperBuilderCustomizer() { @Override public void customize(Jackson2ObjectMapperBuilder builder) { builder.modules(new MyModule()); } };}private static class MyModule extends SimpleModule { @Override public void setupModule(SetupContext context) { // Required, as documented in the Javadoc of SimpleModule super.setupModule(context); context.addDeserializationProblemHandler(new MyDeserializationProblemHandler()); } }private static class MyDeserializationProblemHandler extends DeserializationProblemHandler { @Override public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException { System.out.println("ahahahaa"); return super.handleUnknownProperty(ctxt, p, deserializer, beanOrClass, propertyName); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)