Java Spring JsonDeserializer不适用于类型字符串

Java Spring JsonDeserializer不适用于类型字符串,java,spring,string,jackson,json-deserialization,Java,Spring,String,Jackson,Json Deserialization,我需要从任何对象的字符串类型字段中去掉所有特殊字符和控制字符。已注册反序列化程序,但在运行时从未对字符串执行反序列化程序。 我尝试将其添加为注释@JsonDeserialize(使用=StringProcessorComponent.class),但问题相同。 它适用于任何其他类型,如Date/Long。如果我遗漏了什么,请告诉我 这是我的反序列化程序 @JsonComponent public class StringProcessorComponent extends JsonDeseria

我需要从任何对象的字符串类型字段中去掉所有特殊字符和控制字符。已注册反序列化程序,但在运行时从未对字符串执行反序列化程序。 我尝试将其添加为注释
@JsonDeserialize(使用=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;
    }
}
@JsonComponent
公共类StringProcessorComponent扩展JsonDeserializer{
@凌驾
公共字符串反序列化(JSONP,反序列化上下文ctxt)引发IOException{
JsonToken currentToken=p.getCurrentToken();
if(currentToken.equals(JsonToken.VALUE_字符串)){
String text=MyStringProcessor.clean(p.getValueAsString());
返回文本;
}
返回null;
}
}

要覆盖默认的反序列化程序,可以使用
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'}

谢谢,迈克。除了上面的代码之外,在SpringBoot中注册的以下方法使它工作起来。配置公共类WebConfig实现WebMVCConfiguer{@Override public void extendMessageConverters(List@Raj是的,在<代码> Spring BuooS//Cuff>应用程序中,你需要登记这个模块。你可以用很多不同的方式来做。看看:,如果我的答案是有用的,考虑接受和投票。