如何将依赖注入 Jackson 自定义反序列化器 [英] How to inject dependency into Jackson Custom deserializer

查看:35
本文介绍了如何将依赖注入 Jackson 自定义反序列化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启用一些 String 类型字段的自定义 jackson 反序列化器.反序列化器还需要注入一个基于 guice 的依赖 bean.示例代码如下:

I want to enable a custom jackson deserializer of some fields of type String. The deserializer also needs to be injected with a guice based dependency bean. SampleCode below:

public class CustomDeserializer extends StdDeserializer<String> {

    private SomeDependecy dependency;

    public StringDeserializer() {
        this(null);
    }

    public StringDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return dependency.perform(p.getValueAsString());
    }
}

我无法注册基于类类型的模块,因为它是通用的(String.class、复杂数据类型(但并非每个模块都需要自定义反序列化器)).有没有办法不使用静态方法来实现上述目的?

I cannot register a module based on Class type as it is generic (String.class, Complex Datatype( but not every one require a custome deserializer)). Is there a way to achieve the above without using static methods?

PS:我搜索了网络,但在不使用 statics 的情况下找不到更干净的解决方案.使用一些静态方法来获取上下文和 bean 的所有建议.

PS: I did search net but could not find a cleaner solution without using statics . All the suggestions where around using Some static method to get context and bean.

推荐答案

看起来有另一种方法(感谢我的一位同事)在 objectMapper 实例上使用 injectableValues,然后通过 DeserializationContext ctxt 获取依赖项.以下是代码.

Looks like there is another approach (Thanks to one of my colleague) using injectableValues on the objectMapper instance and then fetch the dependency through DeserializationContext ctxt. Following is the code.

ObjectMapper guice 模块.

ObjectMapper guice module.

public class MerchantConverterModule extends AbstractModule {

    @Override
    protected void configure() {

    }

    @Provides
    @Singleton
    public ObjectMapper objectMapper() {

        ObjectMapper objectMapper = new ObjectMapper();

        /**
         * Add dependency object to object mapper.
         */
        objectMapper.setInjectableValues(new InjectableValues
            .Std()
            .addValue("DependencyName", dependency));

        return objectMapper;
    }


}

自定义解串器的代码

public class CustomDeserializer extends StdDeserializer<String> {

    private SomeDependecy dependency;

    public StringDeserializer() {
        this(null);
    }

    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return getDependency(ctxt).perform(p.getValueAsString());
    }

    private SomeDependency getDependency(DeserializationContext ctxt) {
        SomeDependency dependency = (SomeDependency) ctxt
                .findInjectableValue("DependencyName", null, null);

        return dependency;
    }
}

findInjectableValue 方法是最终方法,因此您可能需要调整单元测试代码以模拟最终结果.

findInjectableValue method is a final method, so you might need to tweak your unit test code to mock finals.

注意: 缺点是对象映射器和解串器之间存在紧密耦合.

NOTE: The drawback is that there is a tight coupling between the objectmapper and deserializer.

这篇关于如何将依赖注入 Jackson 自定义反序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆