如何指示我的端点(或理想情况下,全局:所有端点)在发送之前需要将POJO转换为application/json
格式?
说,我有一个REST控制器,它将端点注入rabbitmq交换,我希望通过这个交换发送的对象被序列化为json,不知何故这样:
@RestController
public class Partners {
@EndpointInject(uri = "rabbitmq:core/partners/create")
private ProducerTemplate partnersCreate;
@RequestMapping("/partners/create")
public Partner create(Partner partner) {
return partnersCreate.requestBody(partner, Partner.class);
}
}
但问题是骆驼显然不知道如何处理POJO:
org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: com.example.core.client.model.Partner to the required type: byte[] with value com.example.core.client.model.Partner@29069cf5
at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:206) ~[camel-core-2.21.1.jar:2.21.1]
at org.apache.camel.component.rabbitmq.RabbitMQMessagePublisher.publish(RabbitMQMessagePublisher.java:86) ~[camel-rabbitmq-2.21.1.jar:2.21.1]
at org.apache.camel.component.rabbitmq.RabbitMQEndpoint.publishExchangeToChannel(RabbitMQEndpoint.java:216) ~[camel-rabbitmq-2.21.1.jar:2.21.1]
at org.apache.camel.component.rabbitmq.RabbitMQProducer$2.doWithChannel(RabbitMQProducer.java:289) ~[camel-rabbitmq-2.21.1.jar:2.21.1]
at org.apache.camel.component.rabbitmq.RabbitMQProducer$2.doWithChannel(RabbitMQProducer.java:286) ~[camel-rabbitmq-2.21.1.jar:2.21.1]
at org.apache.camel.component.rabbitmq.RabbitMQProducer.execute(RabbitMQProducer.java:90) ~[camel-rabbitmq-2.21.1.jar:2.21.1]
at org.apache.camel.component.rabbitmq.RabbitMQProducer.basicPublish(RabbitMQProducer.java:286) ~[camel-rabbitmq-2.21.1.jar:2.21.1]
at org.apache.camel.component.rabbitmq.RabbitMQProducer.processInOut(RabbitMQProducer.java:244) ~[camel-rabbitmq-2.21.1.jar:2.21.1]
at org.apache.camel.component.rabbitmq.RabbitMQProducer.process(RabbitMQProducer.java:193) ~[camel-rabbitmq-2.21.1.jar:2.21.1]
at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:186) ~[camel-core-2.21.1.jar:2.21.1]
at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:86) ~[camel-core-2.21.1.jar:2.21.1]
at org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:541) ~[camel-core-2.21.1.jar:2.21.1]
at org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:506) ~[camel-core-2.21.1.jar:2.21.1]
at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:369) ~[camel-core-2.21.1.jar:2.21.1]
at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:506) ~[camel-core-2.21.1.jar:2.21.1]
at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:264) ~[camel-core-2.21.1.jar:2.21.1]
at org.apache.camel.impl.DefaultProducerTemplate.requestBody(DefaultProducerTemplate.java:325) ~[camel-core-2.21.1.jar:2.21.1]
at com.example.admin.backend.api.Partners.create(Partners.java:16) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
尽管
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>2.21.1</version>
</dependency>
在pom.xml中执行。我的猜测是,除了简单地注册数据格式之外,我必须以某种方式告诉Camel使用它,但目前想知道究竟是怎么回事。
答案 0 :(得分:0)
感谢@ hk6279,通过添加配置类解决了问题:
@Configuration
public class CamelContextConfig implements CamelContextConfiguration {
@Override
public void beforeApplicationStart(CamelContext camelContext) {
}
@Override
public void afterApplicationStart(CamelContext camelContext) {
camelContext.getGlobalOptions().put(JacksonConstants.ENABLE_TYPE_CONVERTER, "true");
camelContext.getGlobalOptions().put(JacksonConstants.TYPE_CONVERTER_TO_POJO, "true");
}
}