rabbitmq consumer在服务器上运行后不接受post请求



我有一个spring引导应用程序,它同时充当生产者和消费者,并做了一些更改。当我作为生产者运行它时,应用程序运行良好,我能够从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]

当我尝试调试执行顺序时,我可以看到正确的执行,直到它到达publishhegateway接口,然后它以上述异常结束。由于该接口使用的是"@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);
}
}
}

输出/InputChannel:

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);
}

您试图发送的消息的json有效负载的序列化似乎有问题。它需要根据TemplateRequest进行验证。

最新更新