apache-camelspring-camel

Apache camel rest dsl RestBindingMode.json is not working


I am trying to build rest api using camel-rest-dsl. I have tried with multiple provider, spark-rest, jetty. But it throwing marshelling exception when i use RestBindingMode.json, if i remove rest binding mode it works fine.

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

    }

}

Here balanceService is a simple Spring @Service with overloaded method and BalanceInfo is simple pojo class with two field and getter setters.

Pom Dependency

    <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>

Exception

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]

Solution

  • Check if you have the dependency camel-jackson included in your project.