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

问题描述

我想启用一些类型为String的字段的自定义杰克逊解串器.解串器还需要注入基于guice的依赖项bean.下面的SampleCode:

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:我确实进行了网上搜索,但是如果不使用static,就找不到更干净的解决方案.使用一些静态方法获取上下文和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;
    }


}

自定义反序列化器的代码

Code of your custom deserializer

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方法是final方法,因此您可能需要调整单元测试代码以模拟final.

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 Custom反序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:38