由以下原因引起:org.apache.camel.NoTypeConversionAvailableException:没有可用的类型转换器将类型:POJO转换为byte []

时间:2018-08-21 10:20:16

标签: java rest maven spring-boot apache-camel

我是Apache Camel的新手。我将REST API与Apache Camel和Spring Boot集成在一起使用。我已经完成了获取请求,它正在按预期工作。但是,在使用POST调用发布数据时,我遇到了一个问题。

我在下面的链接中看到了类似的错误。但是,那对我没有帮助。 org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type:

我尝试了Stackoverflow,youtube和其他网站中提供的几种解决方案。但是,没有运气。您能帮我解决问题吗,

错误说明:

  

org.apache.camel.InvalidPayloadException:没有可用的正文,类型为:   字节[],但值:银行[id = 1,名称= TEST,位置=班加罗尔]   类型:com.ta.common.bean.Bank on:   消息[ID-XDNS01271-1534835668677-10-2]。原因:没有类型转换器   可用于将com.ta.common.bean.Bank类型转换为   必填类型:字节[],值银行[id = 1,名称= TEST,   location =班加罗尔]。交换[ID-XDNS01271-1534835668677-10-1]。造成的   创建者:[org.apache.camel.NoTypeConversionAvailableException-无类型   转换器可用于将com.ta.common.bean.Bank类型转换为   必需的类型:字节[],值银行[id = 1,名称= TEST,   location =班加罗尔]]   org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:117)     在   org.apache.camel.component.netty4.http.DefaultNettyHttpBinding.toNettyRequest(DefaultNettyHttpBinding.java:488)     在   org.apache.camel.component.netty4.http.NettyHttpProducer.getRequestBody(NettyHttpProducer.java:65)     在   org.apache.camel.component.netty4.NettyProducer.process(NettyProducer.java:208)     在   org.apache.camel.component.netty4.http.NettyHttpProducer.process(NettyHttpProducer.java:56)     在   org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)     在   org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:181)     在   org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)     在   org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)     在org.apache.camel.processor.Pipeline.process(Pipeline.java:138)处   org.apache.camel.processor.Pipeline.process(Pipeline.java:101)位于   org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)     在   org.apache.camel.component.direct.DirectBlockingProducer.process(DirectBlockingProducer.java:53)     在   org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)     在   org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:181)     在   org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)     在   org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)     在   org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)     在   org.apache.camel.component.netty4.handlers.ServerChannelHandler.processAsynchronously(ServerChannelHandler.java:141)     在   org.apache.camel.component.netty4.handlers.ServerChannelHandler.channelRead0(ServerChannelHandler.java:112)     在   org.apache.camel.component.netty4.http.handlers.HttpServerChannelHandler.channelRead0(HttpServerChannelHandler.java:213)     在   io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)     在   org.apache.camel.component.netty4.http.handlers.HttpServerMultiplexChannelHandler.channelRead0(HttpServerMultiplexChannelHandler.java:113)     在   io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)     在   io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)     在   io.netty.channel.AbstractChannelHandlerContext.access $ 600(AbstractChannelHandlerContext.java:38)     在   io.netty.channel.AbstractChannelHandlerContext $ 7.run(AbstractChannelHandlerContext.java:353)     在   io.netty.util.concurrent.DefaultEventExecutor.run(DefaultEventExecutor.java:66)     在   io.netty.util.concurrent.SingleThreadEventExecutor $ 5.run(SingleThreadEventExecutor.java:884)     在java.lang.Thread.run(Thread.java:745)   原因:    org.apache.camel.NoTypeConversionAvailableException:没有类型转换器   可用于将com.ta.common.bean.Bank类型转换为   必填类型:字节[],其值为Bank [id = 1,名称= TEST,    location =班加罗尔]   org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:206)     在   org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:115)     ...还有29个

骆驼路由逻辑如下

restConfiguration().component("netty4-http").bindingMode(RestBindingMode.json).host("localhost").port(8000);

rest("/api").consumes("application/json").post("/addbank").type(Bank.class).to("direct:addbank");

from("direct:addbank").log("request body :
 ${body}").to("netty4-http:http://localhost:8181/bank/add?bridgeEndpoint=true").throwException(new
 RuntimeException()).to("direct:cancelAdding");

 from("direct:cancelAdding").log("Cancelled adding bank details");

控制器逻辑为:

@RestController 

@RequestMapping(path="/bank") 

public class BankController {   

     List<Bank> bankList = new ArrayList<>();

     @PostMapping(path="/add",consumes="application/json")
     public List<Bank> addBank(@RequestBody @Valid Bank bank) {
        bankList.add(bank);         
      return bankList;  
     }
 }

Bank.java:

private int id;

private String name;

private String location;

//Getter & Setter methods

1 个答案:

答案 0 :(得分:2)

我无法使用上述代码完全重现该错误。当我尝试上述代码时,这是我遇到的异常。

  

org.apache.camel.processor.binding.BindingException:无法绑定到   json作为消息正文不兼容json。   交换[ID-DESKTOP-OS0TDG5-1534851950401-0-1]

解决此问题是 从

更改休息配置
 restConfiguration().component("netty4-http").host("localhost").bindingMode(RestBindingMode.json)....;

 restConfiguration().component("netty4-http").host("localhost").bindingMode(RestBindingMode.auto)....;

或者您可以完全省略绑定模式。