我有一个spring boot应用程序,它同时充当生产者和消费者,但做了一些更改。当我以生产者身份运行它时,应用程序运行良好,并且我能够从 Postman 那里投递消息(我也可以看到到达rabbitmq交换的消息,尽管我看不到在那里创建的任何消息)。但是,当我以消费者身份运行应用程序时,应用程序运行没有错误。但是,当我尝试投递
2022-06-15 12:45:21.077 ERROR 11464 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Unrecognized token 'TemplateRequest': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (String)"TemplateRequest(templateField=abc)"; line: 1, column: 16]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'TemplateRequest': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (String)"TemplateRequest(templateField=abc)"; line: 1, column: 16], failedMessage=GenericMessage [payload=TemplateRequest(templateField=abc), headers={id=1d60161d-0de4-3b32-7c5f-30a5bf8e6ce0, message=abc, type=io.overledger.springboottemplateservice.dto.TemplateRequest, contentType=application/json, processTime=1655293488245, timestamp=1655293520838}]] with root cause
com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'TemplateRequest': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (String)"TemplateRequest(templateField=abc)"; line: 1, column: 16]
当我试着调试执行序列时,我可以看到正确的执行,直到它到达publishegateway接口,然后它以上面的异常结束。由于这个接口是与“@Gateway(requestChannel = TemplateOutputChannel.CHANNEL_NAME)"一起工作的,所以没有实现类需要调试。
下面是代码:
出版商:
@Configuration
@EnableBinding(TemplateOutputChannel.class)
@IntegrationComponentScan
public class TemplateConfig {
}
消费者:
@Configuration
@EnableBinding(TemplateOutputChannel.class)
@IntegrationComponentScan
@FieldDefaults(level = AccessLevel.PRIVATE)
@AllArgsConstructor
@Slf4j
public class TemplateChannelHandler {
TemplateService templateService;
ObjectMapper objectMapper;
@StreamListener(TemplateOutputChannel.CHANNEL_NAME)
public void stateChannelHandler(@Payload String payload, @Header("message") String message, @Header("processTime") Long processTime, @Header("type") String type) throws JsonProcessingException {
TemplateRequest templateRequest = null;
if (type.equals(TemplateRequest.class.getName())) {
templateRequest = this.objectMapper.readValue(payload, TemplateRequest.class);
}
if (templateRequest != null) {
log.info(String.format("Processing templateRequest: %s, templateField: %s, dispatch timestamp: %d, message type: %s.", templateRequest.getTemplateField(), message, processTime, type));
this.templateService.saveToDatabase(templateRequest);
}
}
}
输出/输入通道:
public interface TemplateOutputChannel {
String CHANNEL_NAME = "template-channel";
// To use as an input channel together with the TemplateChannelHandler, use the @Input annotation instead of @Output.
@Input(TemplateOutputChannel.CHANNEL_NAME)
MessageChannel templateChannel();
}
发布网关:
@MessagingGateway
public interface TemplatePublishGateway {
@Gateway(requestChannel = TemplateOutputChannel.CHANNEL_NAME)
void templatePublishRequest(@Payload TemplateRequest templateRequest, @Header("message") String message, @Header("processTime") Long processTime, @Header("type") String type);
}
1条答案
按热度按时间xt0899hw1#
您尝试发送的消息的json有效负载的序列化似乎有问题。它需要根据您的TemplateRequest进行验证。