gpt4 book ai didi

java - Spring AWS SQS - MessageConversionException

转载 作者:行者123 更新时间:2023-12-01 17:58:02 27 4
gpt4 key购买 nike

我正在尝试解决 SQS 队列的序列化问题,这里是代码:

@Configuration
public class QueueConfig {

@Bean
public QueueMessagingTemplate messagingTemplate(AmazonSQSAsync amazonSqs, ResourceIdResolver resourceIdResolver) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();

DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);

MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setSerializedPayloadClass(String.class);
converter.setObjectMapper(objectMapper);
return new QueueMessagingTemplate(amazonSqs, resourceIdResolver, converter);
}
}

这有效(字符串):

public void publish(String queue, String value) {
messagingTemplate.convertAndSend(queue, value);
}

这不起作用(对象):

public void publish(String queue, Something something) {
messagingTemplate.convertAndSend(queue, something);
}

我收到错误:

org.springframework.messaging.converter.MessageConversionException: Unable to convert payload with type='com.example.Something', contentType='null', converter=[CompositeMessageConverter[converters=[org.springframework.messaging.converter.StringMessageConverter@433f5e00, org.springframework.messaging.converter.MappingJackson2MessageConverter@74e3e86e]]]

我做错了什么?

最佳答案

我怀疑的是这段代码

converter.setSerializedPayloadClass(String.class);

尝试这样做

    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
return new QueueMessagingTemplate(amazonSqs, resourceIdResolver, converter);

这将在将消息发布到队列之前将请求正文转换为 byte[]。

关于java - Spring AWS SQS - MessageConversionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60682729/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com