我有以下代码-

PdfRequest pdfRequest = new PdfRequest();
pdfRequest.setAppName("abc"));
pdfRequest.setId("D342");
String pdfRequestJson = mapper.toJson(pdfRequest);
producerTemplate.requestBodyAndHeaders("direct:sendRoute", pdfRequestJson,
                                                null, String.class);    //line number 154


相同的路由配置是-

from("direct:sendRoute")
            .setExchangePattern(ExchangePattern.InOnly)
            .log("Sending data ${body} to queue") . //this log was never printed
            .toF("pdfQueueProducerBean:topic://%s", topicName)
            .end();


mapper在上述情况下是gson bean

但是在执行此代码时,出现以下错误-

 org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[Message: [Body is null]]
    at org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1379)
    at org.apache.camel.util.ExchangeHelper.extractResultBody(ExchangeHelper.java:622)
    at org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:467)
    at org.apache.camel.impl.DefaultProducerTemplate.sendBodyAndHeaders(DefaultProducerTemplate.java:260)
    at org.apache.camel.impl.DefaultProducerTemplate.requestBodyAndHeaders(DefaultProducerTemplate.java:309)
    at org.apache.camel.impl.DefaultProducerTemplate.requestBodyAndHeaders(DefaultProducerTemplate.java:305)
    at org.apache.camel.impl.DefaultProducerTemplate.requestBodyAndHeaders(DefaultProducerTemplate.java:342)
    at com.tasks.TriggerConsumer.triggerService(TriggerConsumer.java:154)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:407)
    at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:278)
    at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:251)
    at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:166)
    at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:105)
    at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:67)
    at org.apache.camel.impl.ProcessorEndpoint.onExchange(ProcessorEndpoint.java:103)
    at org.apache.camel.impl.ProcessorEndpoint$1.process(ProcessorEndpoint.java:71)
    at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:113)
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
    at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:105)
    at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:87)
    at org.apache.camel.component.jms.EndpointMessageListener.onMessage(EndpointMessageListener.java:103)
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:562)
    at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:500)
    at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:468)
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:325)
    at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:263)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1101)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1093)
    at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:990)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Caused by: java.lang.NullPointerException: null
    at org.apache.camel.impl.DefaultProducerTemplate$2.process(DefaultProducerTemplate.java:254)
    at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:355)
    at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:346)
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:242)
    at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:346)
    at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:201)
    at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:128)
    at org.apache.camel.impl.DefaultProducerTemplate.sendBodyAndHeaders(DefaultProducerTemplate.java:251)
    ... 38 common frames omitted


PdfRequest pojo-

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class PdfRequest {
    private String id;
    private String appName;
    private String timestamp;
}


显然,我的交换主体不为null,因为在通过ProducerTemplate发送对象之前就创建了该对象(如果重要,则会使用默认配置自动绑定producerTemplate)

最佳答案

通过使用producerTemplate.requestBodyAndHeaders(...),您说您想要一个响应,但是通过在路由上设置交换模式InOnly则告诉它不响应。

您的代码看起来实际上不需要响应,因此请尝试:

producerTemplate.sendBody("direct:sendRoute", pdfRequestJson);


代替。

关于java - Exchange正文null异常,即使它在那里,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56040807/

10-10 08:15