我正在尝试使用camel-rest-dsl构建rest api。我尝试过多个提供商,火花休息,码头。但是当我使用 RestBindingMode.json 时它会抛出 marshelling 异常,如果我删除了 rest 绑定模式它工作正常。
SpringRouteBuilder
@Component
public class RestAPIRoutes extends SpringRouteBuilder {
@Override
public void configure() throws Exception {
restConfiguration().component("spark-rest")
.bindingMode(RestBindingMode.json)
.port(8787)
.dataFormatProperty("prettyPrint","true");
rest("/balance").produces("application/json").consumes("application/json")
/* mock api */
.get("/query").route().bean(BalanceService.class,"fetchBalance").endRest()
/* fetch balance by msisdn*/
.get("/query/{msisdn}").description("Fetch line balance by msisdn")
.type(BalanceInfo.class).to("bean:balanceService?method=fetchBalance(${header.msisdn})")
.post("/update").type(BalanceInfo.class).outType(BalanceInfo.class).to("bean:balanceService?method=updateBalance");
}
}
这里的 balanceService 是一个简单的 Spring @Service 重载方法,而 BalanceInfo 是一个简单的 pojo 类,有两个字段和 getter 设置器。
Pom 依赖
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spark-rest</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.22.1</version>
</dependency>
例外
org.apache.camel.processor.binding.BindingException: Cannot bind to json as message body is not json compatible. Exchange[ID-LTB0202777-MAC-1540301942376-3-1]
at org.apache.camel.processor.RestBindingAdvice.unmarshal(RestBindingAdvice.java:317) ~[camel-core-2.22.1.jar:2.22.1]
at org.apache.camel.processor.RestBindingAdvice.before(RestBindingAdvice.java:137) ~[camel-core-2.22.1.jar:2.22.1]