8 Replies Latest reply on Jan 16, 2017 9:20 AM by Madhu Chitti

    Call third party restful web-service using jboss fuse camel

    Sipapp Testing Newbie

      I am new to JBoss fuse. I want to call third party restful web-service using jboss fuse camel. This webservice takes json/form/multi-part as input and return json/form/multi-part as output. Please can anyone point to any working example.  I referred many documentation and blogs but getting confused more as browse more.

        • 2. Re: Call third party restful web-service using jboss fuse camel
          Sipapp Testing Newbie

          Thanks Madhu for pointer..

          Got little more information here but looking for some basic working end-to-end example.

           

          Any help appreciated.

          • 3. Re: Call third party restful web-service using jboss fuse camel
            Madhu Chitti Newbie

            Below is the sample camel context

             

            <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

                    <dataFormats>

                        <json id="jsonToPojo" library="Jackson" prettyPrint="true"/>

                    </dataFormats>

                    <restConfiguration bindingMode="json" component="restlet" port="8080">

                    </restConfiguration>

             

                    <rest consumes="application/json" path="/callService" produces="application/json">

                        <post type="com.mycompany.JsonToPojo" uri="/uri">

                            <to uri="direct:status"/>

                        </post>

                    </rest>

             

                    <route id="_route1">

                        <from id="_from1" uri="direct:status"/>

                        <log id="_log2" loggerRef="myLogger" message="${body}"/>

                        <to id="_to1" uri="restlet:{{endpoint_url}}?restletMethods=PUT"/>

                        <log id="_log2" loggerRef="myLogger" message="After  ${body}"/>

                    </route>

            </camelContext>

             

            In the above example I am trying to consume a rest service and it worked fine for me. Hope this is helpful.

            For reference : http://camel.apache.org/rest-dsl

            1 of 1 people found this helpful
            • 4. Re: Call third party restful web-service using jboss fuse camel
              Sipapp Testing Newbie

              Thanks Madhu,

               

              I have followed your post. Please find below context.xml details:

               

              <?xml version="1.0" encoding="UTF-8"?>

              <!-- Configures the Camel Context -->

              <beans xmlns="http://www.springframework.org/schema/beans"

                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                xsi:schemaLocation="         http://camel.apache.org/schema/spring          http://camel.apache.org/schema/spring/camel-spring.xsd         http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context          http://www.springframework.org/schema/context/spring-context.xsd">

               

                <camelContext id="amq-example-context" xmlns="http://camel.apache.org/schema/spring"

                xmlns:order="http://com.mycompany/examples/order">

                <restConfiguration bindingMode="auto" component="restlet"

                port="443" />

               

                <route id="file-to-jms-route">

                <!-- from id="incomingFolder" uri="file:work/in"/ -->

                <!-- to id="sendIncomingOrder" uri="activemq:incomingOrders"/ -->

                <from id="_from1" uri="direct:status" />

                <to id="_to2"

                uri="restlet:https://sample-rest-service.com:443/api/v15.0/objects/user/427?exchangePattern=InOut&restletMethod=POST&restletRealm=Authorization" />

                <to id="_to1" uri="file:C:\workspace\jbdevstudio\restfuloutput" />

                </route>

                </camelContext>

              </beans>

               

              Adding image of my folder structure and its contents to have more idea how i have done it:

              example.jpg

               

              After deploying the bundle using osgi:install i am getting below exception:

               

              JBossFuse:karaf@root> Exception in thread "SpringOsgiExtenderThread-4" org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed

              to create route file-to-jms-route at: >>> To[restlet:https://sample-rest-service.com:443/api/v15.0/objects/user/427?exchangePattern=InOut&restletMethod=POST&restletR

              ealm=Authorization] <<< in route: Route(file-to-jms-route)[[From[direct:status]] -> [To[restle... because of Failed to resolve endpoint: restlet://https://sample-res

              t-service.com:443/api/v15.0/objects/user/427?exchangePattern=InOut&restletMethod=POST&restletRealm=Authorization due to: No component found with scheme: restlet

                      at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1690)

                      at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:138)

                      at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:340)

                      at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:96)

                      at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:334)

                      at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:954)

                      at org.springframework.osgi.context.support.AbstractOsgiBundleApplicationContext.finishRefresh(AbstractOsgiBundleApplicationContext.java:235)

                      at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:358)

                      at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)

                      at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:32

              0)

                      at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplica

              tionContextExecutor.java:132)

                      at java.lang.Thread.run(Thread.java:745)

              Caused by: org.apache.camel.FailedToCreateRouteException: Failed to create route file-to-jms-route at: >>> To[restlet:https://sample-rest-service.com:443/api/v15.0/o

              bjects/user/427?exchangePattern=InOut&restletMethod=POST&restletRealm=Authorization] <<< in route: Route(file-to-jms-route)[[From[direct:status]] -> [To[restle... be

              cause of Failed to resolve endpoint: restlet://https://sample-rest-service.com:443/api/v15.0/objects/user/427?exchangePattern=InOut&restletMethod=POST&restletRealm=A

              uthorization due to: No component found with scheme: restlet

                      at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1072)

                      at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:196)

                      at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:974)

                      at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:3301)

                      at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3024)

                      at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:175)

                      at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2854)

                      at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2850)

                      at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:2873)

                      at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:2850)

                      at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)

                      at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:2819)

                      at org.apache.camel.spring.SpringCamelContext.maybeStart(SpringCamelContext.java:275)

                      at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:136)

                      ... 10 more

              Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: restlet://https://sample-rest-service.com:443/api/v15.0/objects/user/427?exch

              angePattern=InOut&restletMethod=POST&restletRealm=Authorization due to: No component found with scheme: restlet

                      at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:594)

                      at org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:79)

                      at org.apache.camel.model.RouteDefinition.resolveEndpoint(RouteDefinition.java:211)

                      at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:107)

                      at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:113)

                      at org.apache.camel.model.SendDefinition.resolveEndpoint(SendDefinition.java:62)

                      at org.apache.camel.model.SendDefinition.createProcessor(SendDefinition.java:56)

                      at org.apache.camel.model.ProcessorDefinition.makeProcessorImpl(ProcessorDefinition.java:534)

                      at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:495)

                      at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:219)

                      at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1069)

                      ... 23 more

              • 5. Re: Call third party restful web-service using jboss fuse camel
                Madhu Chitti Newbie

                Try running features:install camel-restlet from karaf prompt

                • 6. Re: Call third party restful web-service using jboss fuse camel
                  Madhu Chitti Newbie

                  I checked your code, the below part from my example is missing in your code,

                   

                       <rest consumes="application/json" path="/callService" produces="application/json">

                              <post type="com.mycompany.JsonToPojo" uri="/uri">

                                  <to uri="direct:status"/>

                              </post>

                          </rest>

                   

                  Without the above code, the <from id="_from1" uri="direct:status" /> from your camel context file will not be invoked.

                  1 of 1 people found this helpful
                  • 7. Re: Call third party restful web-service using jboss fuse camel
                    Sipapp Testing Newbie

                    Thank a lot Madhu...

                     

                    Your pointers helped me..

                     

                    Finally, I can call rest service below is my snippet:

                     

                    <?xml version="1.0" encoding="UTF-8"?>

                    <!-- Configures the Camel Context -->

                    <beans xmlns="http://www.springframework.org/schema/beans"

                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="         http://camel.apache.org/schema/spring          http://camel.apache.org/schema/spring/camel-spring.xsd         http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context          http://www.springframework.org/schema/context/spring-context.xsd">

                        <bean class="org.apache.activemq.camel.component.ActiveMQComponent" id="activemq">

                            <property name="brokerURL" value="tcp://localhost:61616"/>

                            <property name="userName" value="admin"/>

                            <property name="password" value="admin"/>

                            <property name="usePooledConnection" value="false"/>

                        </bean>

                        <camelContext id="amq-example-context"

                            xmlns="http://camel.apache.org/schema/spring" xmlns:order="http://com.mycompany/examples/order">

                            <dataFormats>

                                <json id="docsDocs" library="Jackson" unmarshalTypeName="com.mycompany.pojos.MyPojo"/>

                            </dataFormats>

                            <restConfiguration bindingMode="auto" component="restlet" port="443"/>

                            <route id="file-to-jms-route">

                                <!-- from id="incomingFolder" uri="file:work/in"/ -->

                                <!-- to id="sendIncomingOrder" uri="activemq:incomingOrders"/ -->

                                <from id="_to3" uri="timer:order?fixedRate=true&amp;period=5000&amp;delay=2000"/>

                                <to id="_to2" uri="restlet:https://my-service.com:443/api/v1.0/user/427?exchangePattern=InOut&restletMethod=GET&auth=1234abcd5678pqrs"/>

                                <to id="_to1" uri="file:C:\workspace\jbdevstudio\restfuloutput"/>

                                <to id="sendIncomingDocs" uri="activemq:queue:incomingData"/>

                            </route>

                            <route id="jms-cbr-route" streamCache="true">

                                <from id="listenToIncomingData" uri="activemq:queue:incomingData"/>

                                <unmarshal id="_unmarshal1">

                                    <json allowJmsType="true" library="Jackson"/>

                                </unmarshal>

                                <log id="logEndProcessing" message="Done processing parsing ${body}"/>

                                <to id="myQData" uri="activemq:queue:myQData"/>

                            </route>

                        </camelContext>

                    </beans>

                     

                     

                    Not sure whether its a standard approach but now i am able to call service (Any comments?).

                    Also we have multiple paths like, is there any way to configure multiple end points in standard way:

                    https://my-service.com:443/api/v1.0/country

                    https://my-service.com:443/api/v1.0/country/{id}/state/{id2}

                    https://my-service.com:443/api/v1.0/education

                    https://my-service.com:443/api/v1.0/roles

                     

                    Also in this service call i am sending one header (auth=1234abcd5678pqrs) in query string. How to set using setHeader also any idea about setBody (for multipart file objects and form-data ) I want to get the json object from AMQ, marshal it to entity class and store in database.

                    • 8. Re: Call third party restful web-service using jboss fuse camel
                      Madhu Chitti Newbie

                      Before calling the service you can set the header as below

                       

                      <setHeader headerName="auth">

                          <constant>1234abcd5678pqrs</constant>      

                        </setHeader>

                       

                      For  multipart file objects and form-data refer below link (camel-jetty handles the multi-part form by default)

                       

                      http://camel.apache.org/jetty.html

                       

                      Please see the below link for converting JSON to POJO

                       

                      http://camel.apache.org/json.html

                       

                      Note : Since you are consuming your service using timer componenet, below code is not required in your context file, this code is required if you want to expose your service using REST DSL.

                       

                              <restConfiguration bindingMode="auto" component="restlet" port="443"/>