【问题标题】:Spring JsonDeserializer not working for the type StringSpring JsonDeserializer 不适用于 String 类型
【发布时间】:2020-06-28 16:29:15
【问题描述】:

我需要从任何对象的字符串类型字段中去除所有特殊字符和控制字符。反序列化程序已注册,但在 Strings 的运行时期间从未执行。 我尝试添加与注释相同的 @JsonDeserialize(using = StringProcessorComponent.class),但同样的问题。 它适用于任何其他类型,例如Date/Long。如果我遗漏任何内容,请告诉我。

这是我的反序列化器。

@JsonComponent
public class StringProcessorComponent extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonToken currentToken = p.getCurrentToken();

        if (currentToken.equals(JsonToken.VALUE_STRING)) {
            String text = MyStringProcessor.clean(p.getValueAsString());
            return text;
        }

        return null;
    }
}

【问题讨论】:

    标签: java spring string jackson json-deserialization


    【解决方案1】:

    要覆盖默认反序列化器,您可以使用SimpleModule。此外,当您想尽可能扩展默认实现时,您可以扩展默认反序列化器。在您的情况下,您可以扩展 com.fasterxml.jackson.databind.deser.std.StringDeserializer 类。见下例:

    import com.fasterxml.jackson.core.JsonParser;
    import com.fasterxml.jackson.databind.DeserializationContext;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
    import com.fasterxml.jackson.databind.module.SimpleModule;
    
    import java.io.IOException;
    import java.util.StringJoiner;
    
    public class JsonApp {
    
        public static void main(String[] args) throws Exception {
            SimpleModule stringModule = new SimpleModule("String Module");
            stringModule.addDeserializer(String.class, new CustomStringDeserializer());
    
            ObjectMapper mapper = new ObjectMapper();
            mapper.registerModule(stringModule);
    
            String json = "{\"firstName\":\"  Tom \",\"lastName\":\"  Long \"}";
    
            CustomStringPojo customStringPojo = mapper.readValue(json, CustomStringPojo.class);
            System.out.println(customStringPojo);
        }
    }
    
    class CustomStringDeserializer extends StringDeserializer {
        @Override
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
            String text = super.deserialize(p, ctxt);
            //clean up value
            return text.trim();
        }
    }
    
    class CustomStringPojo {
        private String firstName;
        private String lastName;
    
        // getters, setters, toString
    }
    

    上面的代码打印:

    CustomStringPojo{firstName='Tom', lastName='Long'}
    

    【讨论】:

    • 谢谢,迈克。除了上面的代码之外,在 Spring Boot 中的注册方式使其工作。配置公共类 WebConfig 实现 WebMvcConfigurer { @Override public void extendMessageConverters(List> 转换器) { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(stringProcessorModule()); } public SimpleModule stringProcessorModule() { SimpleModule stringModule = new SimpleModule(); stringModule.addDeserializer(String.class, new MyStringDeserializer());返回字符串模块; } }
    • @Raj,是的,在Spring Boot 应用程序中,您需要注册此模块。你可以通过许多不同的方式做到这一点。看看:SpringBoot: Consume & Produce XML with a Custom Serializer + DeserializerHow to enable 'ALLOW_NUMERIC_LEADING_ZEROS' feature to allow leading zeroes in...。如果我的回答有帮助,请考虑接受并投票。
    猜你喜欢
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    相关资源
    最近更新 更多