程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了指定自定义 ObjectMapper 时忽略 spring.json.type.mapping 属性大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决指定自定义 ObjectMapper 时忽略 spring.json.type.mapping 属性?

开发过程中遇到指定自定义 ObjectMapper 时忽略 spring.json.type.mapping 属性的问题如何解决?下面主要结合日常开发的经验,给出你关于指定自定义 ObjectMapper 时忽略 spring.json.type.mapping 属性的解决方法建议,希望对你解决指定自定义 ObjectMapper 时忽略 spring.json.type.mapping 属性有所启发或帮助;

我在将自定义 ObjectMapper 注入 Spring Kafka 序列化程序时遇到了问题,我已使用此 answer 解决了该问题,LocaldatetiR_883_11845@e 正在以正确的模式进行序列化。

@Configuration
public class KafkaCustomizer implements DefaultKafkaProducerFactoryCustomizer {

    @Bean
    public ObjectMapper objectMapper() {
        var mapper = new ObjectMapper();
        var module = new JavaTimeModule();
        var serializer = new LocaldatetiR_883_11845@eserializer(
                datetiR_883_11845@eFormatter.ofPattern(DateConstants.datetiR_883_11845@E_FORMAT_PATTERN));
        module.addserializer(LocaldatetiR_883_11845@e.class,serializer);
        mapper.registerModule(modulE);
        mapper.disable(serializationFeature.WRITE_DATES_AS_timestAMPS);
        return mapper;
    }

    @OverrIDe
    public voID customize(DefaultKafkaProducerFactory<?,?> producerFactory) {
        producerFactory.SETVALueserializer(new Jsonserializer<>(objectMapper()));
    }

}

但现在我面临另一个问题,spring.kafka.producer.propertIEs.spring.Json.type.mapPing 属性被忽略。

我记录的 __TypEID__ 标头是用 FQCN 设置的,而不是我在 spring.Json.type.mapPing 属性中放入的令牌:foo > com.foo.package.Foo

当我进行调试时,似乎没有调用 configure 类的 org.springframework.kafka.support.serializer.Jsonserializer 方法:

@OverrIDe
public voID configure(Map<String,?> configs,Boolean isKey) {
    ...
    if (configs.containsKey(TYPE_MAPPingS) && !this.typeMapperExplicitlySet
            && this.typeMapper instanceof AbstractJavaTypeMapper) {
        ((AbstractJavaTypeMapper) this.typeMapper)
                .setIDClassmapPing(createMapPings((String) configs.get(TYPE_MAPPingS)));
    }
}

但是当我禁用自定义时

    @OverrIDe
    public voID customize(DefaultKafkaProducerFactory<?,?> producerFactory) {
        // producerFactory.SETVALueserializer(new Jsonserializer<>(objectMapper()));
    }

然后使用正确的标记设置 __TypEID__ 标头但正如预期的那样,我使用自定义 ObjectMapper 取消了日期格式

那么如何处理这整个情况?

解决方法

如果您自己做 private static List<String> getData(int attribute1){ String uri="localhost:9191/getData/"+attribute1; private RestTemplate restTemplate = new RestTemplate(); List<String> res = restTemplate.getForObject(uri,List.class); return res; } ,则您需要自己为它提供适当的生产者配置。当 serialized 的实例不受 Kafka Client 控制时,不会调用该 new Jsonserializer<>

我想说在你的情况下可以这样做:

configure()

文档中有一些信息:https://docs.spring.io/spring-kafka/docs/current/reference/html/#tip-json,但可能我们需要为程序化配置案例扩展它...

大佬总结

以上是大佬教程为你收集整理的指定自定义 ObjectMapper 时忽略 spring.json.type.mapping 属性全部内容,希望文章能够帮你解决指定自定义 ObjectMapper 时忽略 spring.json.type.mapping 属性所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。