Is it possible to use a custom serializer/deserializer for a type by default in spring?

huangapple go评论81阅读模式
英文:

Is it possible to use a custom serializer/deserializer for a type by default in spring?

问题

我从第三方库(来自jooqJSONB)中拥有一种类型,为此我编写了自定义的序列化程序/反序列化程序:

@JsonComponent
public class JSONBSerializer extends JsonSerializer<JSONB> {
    @Override
    public void serialize(JSONB jsonb, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeString(jsonb.toString());
    }
}
@JsonComponent
public class JSONBDeserializer extends JsonDeserializer<JSONB> {
    @Override
    public JSONB deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        return JSONB.valueOf(jsonParser.getValueAsString());
    }
}

我想知道是否有一种方法可以告诉Spring或Jackson在不必在项目中的每个JSONB字段上注释@JsonSerialize(using = JSONBSerializer.class)@JsonDeserialize(using = JSONBDeserializer.class)的情况下,默认使用它们?

英文:

I have a type from a third party library (JSONB from jooq) that I've written a custom serializer/deserializer for:

@JsonComponent
public class JSONBSerializer extends JsonSerializer&lt;JSONB&gt; {
    @Override
    public void serialize(JSONB jsonb, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeString(jsonb.toString());
    }
}
@JsonComponent
public class JSONBDeserializer extends JsonDeserializer&lt;JSONB&gt; {
    @Override
    public JSONB deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        return JSONB.valueOf(jsonParser.getValueAsString());
    }
}

I am wondering if there is a way to tell spring or jackson to use these by default without having to annotate every JSONB field in the project with @JsonSerialize(using = JSONBSerializer.class) and @JsonDeserialize(using = JSONBDeserializer.class)?

答案1

得分: 1

你需要创建一个新的com.fasterxml.jackson.databind.module.SimpleModule实例,并注册所有自定义的序列化器和反序列化器。接下来,您需要查看如何在您的Spring Boot版本中注册新的自定义模块。

@Bean
public SimpleModule jooqModule() {
    SimpleModule jooqModule = new SimpleModule();
    jooqModule.addSerializer(JSONB.class, new JSONBSerializer());
    jooqModule.addDeserializer(JSONB.class, new JSONBDeserializer());
}

请参考以下内容:

英文:

You need to create new com.fasterxml.jackson.databind.module.SimpleModule instance and register all custom serialisers and deserialisers. Next, you need to check out how to register new custom module in your version of Spring Boot.

@Bean
public SimpleModule jooqModule() {
	SimpleModule jooqModule = new SimpleModule();
	jooqModule.addSerializer(JSONB.class, new JSONBSerializer());
	jooqModule.addDeserializer(JSONB.class, new JSONBDeserializer());
}

Take a look at:

huangapple
  • 本文由 发表于 2020年10月24日 02:13:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64505346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定