Discussion:
Apache camel rest dsl RestBindingMode.json is throwing exception
rahul vashishth
2018-10-23 14:03:12 UTC
Permalink
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 then it
works fine with string type.

I am using version 2.22.1

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


Please help

Rahul
Valdis Andersons
2018-10-25 08:21:18 UTC
Permalink
Hi Rahul,

Could you try simplifying the route to determine where it's starting to break down for you? Initially just declare something trivial with just the endpoint, mapping and processor.

restConfiguration().component("spark-rest").bindingMode(RestBindingMode.json);
rest("/balance").post("/update")
.type(BalanceInfo.class)
.route()
.process(this::process)
.endRest();

Call you bean from the process method and see if that still works. If it's working, then start adding the other configuration bits one by one and see where things start falling over, that might help track things down faster.


Regards,
Valdis

-----Original Message-----
From: rahul vashishth [mailto:***@gmail.com]
Sent: 23 October 2018 15:03
To: ***@camel.apache.org
Subject: Apache camel rest dsl RestBindingMode.json is throwing exception

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 then it works fine with string type.

I am using version 2.22.1

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


Please help

Rahul

Vhi Group DAC (Vhi) is a holding company for insurance and healthcare services, which include Vhi Healthcare DAC, Vhi Insurance DAC, Vhi Health Services DAC and Vhi Investments DAC. Vhi Healthcare DAC trading as Vhi Healthcare and Vhi Insurance DAC trading as Vhi Insurance are regulated by the Central Bank of Ireland. Vhi Healthcare is tied to Vhi Insurance DAC for health insurance in Ireland which is underwritten by Vhi Insurance DAC. Vhi Healthcare is tied to Zurich Life Assurance plc for Vhi Life Term Insurance and Vhi Mortgage Protection which is underwritten by Zurich Life Assurance plc. Vhi Healthcare is tied to Collinson Insurance Services Limited for MultiTrip Travel Insurance, Backpacker Travel Insurance and Vhi Dental Insurance which are underwritten by Great Lakes Insurance SE, UK branch and for Vhi Canada Cover and Vhi International Health Insurance which are underwritten by Astrenska Insurance Limited. For more information about the Vhi Group please go to: https://www.vhi.ie/about-vhi.


Tá Vhi Group DAC (Vhi) ina chuideachta sealbhaíochta le haghaidh seirbhísí árachais agus seirbhísí cúram sláinte, lena n-áirítear Vhi Healthcare DAC, Vhi Insurance DAC, Vhi Health Services DAC agus Vhi Investments DAC. Déanann Banc Ceannais na hÉireann rialáil ar Vhi Healthcare DAC, ag trádáil dó mar Vhi Healthcare, agus ar Vhi Insurance DAC, ag trádáil dó mar Vhi Insurance. Tá Vhi Healthcare ceangailte le Vhi Insurance DAC le haghaidh árachas sláinte in Éirinn, rud atá frithgheallta ag Vhi Insurance DAC. Tá Vhi Healthcare ceangailte le Zurich Life Assurance plc le haghaidh Árachais Saoil de chuid Vhi agus Árachas Cosanta Morgáiste de chuid Vhi atá frithgheallta ag Zurich Life Assurance plc. Tá Vhi Healthcare ceangailte le Collinson Insurance Services Limited le haghaidh Árachas Taistil Ilturais agus Turasóirí Mála Droma agus Árachas Fiaclóireachta de chuid Vhi atá frithgheallta ag Great Lakes Insurance SE, UK branch agus le haghaidh Clúdach Cheanada de chuid Vhi agus Árachas Sláinte Idirnáisiúnta de chuid Vhi atá frithgheallta ag Astrenska Insurance Limited. Chun tuilleadh faisnéise a fháil faoi Ghrúpa Vhi, tabhair cuairt ar: https://www.vhi.ie/about-vhi.

This e-mail and any files transmitted with it contain information which may be confidential and which may also be privileged and is intended solely for the use of the individual or entity to whom it is addressed. Unless you are the intended recipient you may not copy or use it, or disclose it to anyone else. Any opinions expressed are that of the individual and not necessarily that of the Vhi Group. If you have received this e-mail in error
Loading...