Code of calling rest service is not working

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Code of calling rest service is not working

Jaiprakash Mewada
Hi Team,

I am beginner in apache camel. I want to make to rest call but not able to
get expected result. Can someone help me, what exact change I need to do in
below code in order to receive the json response.











































*import org.apache.camel.CamelContext;import
org.apache.camel.ConsumerTemplate;import org.apache.camel.Exchange;import
org.apache.camel.Processor;import org.apache.camel.ProducerTemplate;import
org.apache.camel.builder.RouteBuilder;import
org.apache.camel.impl.DefaultCamelContext;public class
CamelHelloWorldExample { public static void main(String[] args) throws
Exception { CamelContext context = new DefaultCamelContext();
context.setTracing(true); context.addRoutes(new RouteBuilder() { @Override
public void configure() throws Exception { System.out.println("-------Start
of RouteBuilder-----------");
from("direct:start").setHeader(Exchange.HTTP_METHOD, simple("GET"))
.to("http://dummy.restapiexample.com/api/v1/employee/330
<http://dummy.restapiexample.com/api/v1/employee/330>").process(new
Processor() { public void process(Exchange exchange) throws Exception {
System.out.println(exchange.getIn().getBody(String.class)); } } )
.to("seda:end"); System.out.println("-------End of
RouteBuilder-----------"); } } ); context.start(); ProducerTemplate
prodTemp = context.createProducerTemplate();
prodTemp.sendBody("direct:start", "Hello"); ConsumerTemplate consTemp =
context.createConsumerTemplate(); String msg =
consTemp.receiveBody("seda:end", String.class); System.out.println(msg); }}*

--
*Thanks & Regards,*
*Jaiprakash Mewada*
Senior Enterprise Software Engineer

*HotWax Systems*
Reply | Threaded
Open this post in threaded view
|

Re: Code of calling rest service is not working

Omar Al-Safi
I am new to Camel as well, I was figuring things on the way :). First you
need to use a REST component, you can use the REST DSL
<https://camel.apache.org/rest-dsl.html>, however this is only a DSL not a
actual http server, you will need to include an http server component as
well (e.g:  camel-netty4-http, camel-restlet, camel-spark-rest ..etc), once
you included it, you will need to tell Camel to use the specified component
through the configuration RouteBuilder
(e.g: restConfiguration().component("spark-rest").port(8000);). Also in
your above example, the JVM will terminate immediately, and hence Camel
will stop immediately. Therefore you need to either use the Main class that
is shipped in Camel Core (example
<https://github.com/apache/camel/blob/master/examples/camel-example-main/src/main/java/org/apache/camel/example/MyApplication.java#L31>)
or keep the JVM running indefinitely by adding
(Thread.sleep(Integer.MAX_VALUE);)

You can take a look here for an example:
https://www.codepile.net/pile/p4dDJd4x

Please note I also included the camel-spark-rest component in the POM file
as well.

On Mon, 29 Jul 2019 at 14:06, Jaiprakash Mewada <
[hidden email]> wrote:

> Hi Team,
>
> I am beginner in apache camel. I want to make to rest call but not able to
> get expected result. Can someone help me, what exact change I need to do in
> below code in order to receive the json response.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> *import org.apache.camel.CamelContext;import
> org.apache.camel.ConsumerTemplate;import org.apache.camel.Exchange;import
> org.apache.camel.Processor;import org.apache.camel.ProducerTemplate;import
> org.apache.camel.builder.RouteBuilder;import
> org.apache.camel.impl.DefaultCamelContext;public class
> CamelHelloWorldExample { public static void main(String[] args) throws
> Exception { CamelContext context = new DefaultCamelContext();
> context.setTracing(true); context.addRoutes(new RouteBuilder() { @Override
> public void configure() throws Exception { System.out.println("-------Start
> of RouteBuilder-----------");
> from("direct:start").setHeader(Exchange.HTTP_METHOD, simple("GET"))
> .to("http://dummy.restapiexample.com/api/v1/employee/330
> <http://dummy.restapiexample.com/api/v1/employee/330>").process(new
> Processor() { public void process(Exchange exchange) throws Exception {
> System.out.println(exchange.getIn().getBody(String.class)); } } )
> .to("seda:end"); System.out.println("-------End of
> RouteBuilder-----------"); } } ); context.start(); ProducerTemplate
> prodTemp = context.createProducerTemplate();
> prodTemp.sendBody("direct:start", "Hello"); ConsumerTemplate consTemp =
> context.createConsumerTemplate(); String msg =
> consTemp.receiveBody("seda:end", String.class); System.out.println(msg);
> }}*
>
> --
> *Thanks & Regards,*
> *Jaiprakash Mewada*
> Senior Enterprise Software Engineer
>
> *HotWax Systems*
>
Reply | Threaded
Open this post in threaded view
|

Re: Code of calling rest service is not working

Jean-Baptiste Onofré
You can also use camel-cxfrs for rest server and/or client.

Regards
JB

Le 29 juil. 2019 à 15:36, à 15:36, Omar Al-Safi <[hidden email]> a écrit:

>I am new to Camel as well, I was figuring things on the way :). First
>you
>need to use a REST component, you can use the REST DSL
><https://camel.apache.org/rest-dsl.html>, however this is only a DSL
>not a
>actual http server, you will need to include an http server component
>as
>well (e.g:  camel-netty4-http, camel-restlet, camel-spark-rest ..etc),
>once
>you included it, you will need to tell Camel to use the specified
>component
>through the configuration RouteBuilder
>(e.g: restConfiguration().component("spark-rest").port(8000);). Also in
>your above example, the JVM will terminate immediately, and hence Camel
>will stop immediately. Therefore you need to either use the Main class
>that
>is shipped in Camel Core (example
><https://github.com/apache/camel/blob/master/examples/camel-example-main/src/main/java/org/apache/camel/example/MyApplication.java#L31>)
>or keep the JVM running indefinitely by adding
>(Thread.sleep(Integer.MAX_VALUE);)
>
>You can take a look here for an example:
>https://www.codepile.net/pile/p4dDJd4x
>
>Please note I also included the camel-spark-rest component in the POM
>file
>as well.
>
>On Mon, 29 Jul 2019 at 14:06, Jaiprakash Mewada <
>[hidden email]> wrote:
>
>> Hi Team,
>>
>> I am beginner in apache camel. I want to make to rest call but not
>able to
>> get expected result. Can someone help me, what exact change I need to
>do in
>> below code in order to receive the json response.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *import org.apache.camel.CamelContext;import
>> org.apache.camel.ConsumerTemplate;import
>org.apache.camel.Exchange;import
>> org.apache.camel.Processor;import
>org.apache.camel.ProducerTemplate;import
>> org.apache.camel.builder.RouteBuilder;import
>> org.apache.camel.impl.DefaultCamelContext;public class
>> CamelHelloWorldExample { public static void main(String[] args)
>throws
>> Exception { CamelContext context = new DefaultCamelContext();
>> context.setTracing(true); context.addRoutes(new RouteBuilder() {
>@Override
>> public void configure() throws Exception {
>System.out.println("-------Start
>> of RouteBuilder-----------");
>> from("direct:start").setHeader(Exchange.HTTP_METHOD, simple("GET"))
>> .to("http://dummy.restapiexample.com/api/v1/employee/330
>> <http://dummy.restapiexample.com/api/v1/employee/330>").process(new
>> Processor() { public void process(Exchange exchange) throws Exception
>{
>> System.out.println(exchange.getIn().getBody(String.class)); } } )
>> .to("seda:end"); System.out.println("-------End of
>> RouteBuilder-----------"); } } ); context.start(); ProducerTemplate
>> prodTemp = context.createProducerTemplate();
>> prodTemp.sendBody("direct:start", "Hello"); ConsumerTemplate consTemp
>=
>> context.createConsumerTemplate(); String msg =
>> consTemp.receiveBody("seda:end", String.class);
>System.out.println(msg);
>> }}*
>>
>> --
>> *Thanks & Regards,*
>> *Jaiprakash Mewada*
>> Senior Enterprise Software Engineer
>>
>> *HotWax Systems*
>>