You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by chitra1986 <ch...@gmail.com> on 2015/01/26 17:07:54 UTC

Calling REST service without using spring

Hi,

I am new to camel .When I try to call REST service using camel , I am
getting null as response.I don't know where I am missing. 
*camel-config.xml:*
<routes xmlns="http://camel.apache.org/schema/spring">

	<route id="fromServlet">
		<from uri="servlet:///getPak" />

			<to uri="bean:pakBean"/> 
	</route> 

*Web.xml:*
<context-param>
    <param-name>name</param-name>
    <param-value>MyCamel</param-value>
  </context-param>

   
  <context-param>
    <param-name>routeBuilder-MyRoute</param-name>
    
    <param-value>classpath:camel-config.xml</param-value>
  </context-param>
  <context-param>
     
    <param-name>CamelContextLifecycle</param-name>
   
<param-value>main.java.org.apache.camel.example.servletlistener.MyLifecycle</param-value>
  </context-param>

   
   <listener>
   
<listener-class>org.apache.camel.component.servletlistener.JndiCamelServletContextListener</listener-class>
  </listener>

  
  <servlet>
    <servlet-name>CamelServlet</servlet-name>
   
<servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

   
  <servlet-mapping>
    <servlet-name>CamelServlet</servlet-name>
    <url-pattern>/camel/*</url-pattern>
  </servlet-mapping> 

*MyLifeCycle.java:*
public class MyLifecycle implements CamelContextLifecycle<JndiRegistry> {

public void beforeStart(ServletCamelContext camelContext, JndiRegistry
registry)
			throws Exception {
		registry.bind("pakBean", new TestRest());
	}
.....
}

TestRest.java:
public class TestRest
 
{
    int status = 0;
    String problemDescription = null;
    String Response=null;
    String details = null;
   String
URL1="http://localhost:8080/rest/services/token/fetchTest?authMethod=Basic&authUsername=test&authPassword=Test";
  public String test(){
	  CamelContext camel = new DefaultCamelContext();
	    ProducerTemplate template = camel.createProducerTemplate();
	    Exchange exchange = template.request(URL1, new Processor() {
	            public void process(Exchange exchange) throws Exception {
	              exchange.getIn().setHeader(Exchange.HTTP_URI,URL1);
	              exchange.getIn().setHeader(Exchange.HTTP_METHOD,"GET");
	              exchange.getIn().setHeader(Exchange.AUTHENTICATION,"Basic");
	             
exchange.getIn().setHeader(Exchange.CONTENT_TYPE,"application/json");
	              exchange.getIn().setBody("Hello");
	              String uri = exchange.getIn().getHeader(Exchange.HTTP_URI,
String.class);
	              String out = exchange.getIn().getHeader(Exchange.HTTP_METHOD,
String.class);
	              System.out.println("uri:::"+uri);
	              System.out.println("out:::"+out);
	            }
	           
	    });
	    Message out = exchange.getOut();//Getting null and it is not hitting
the service
	   
  }
    
    
}

Please help me to fix the issue.Thanks in adv.




--
View this message in context: http://camel.465427.n5.nabble.com/Calling-REST-service-without-using-spring-tp5762125.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Calling REST service without using spring

Posted by Willem Jiang <wi...@gmail.com>.
Hi,

I don’t think you specify the right CamelContextLifecycle value.
It should be “org.apache.camel.example.servletlistener.MyLifecycle" instead of “main.java.org.apache.camel.example.servletlistener.MyLifecycle”

The server should be accessed from "http://localhost:8080/CONTEXT_PATH/camel/getPak”, CONTEXT_PATH is the war file name.

--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.iteye.com (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem



On January 27, 2015 at 12:23:25 AM, chitra1986 (chitra1986@gmail.com) wrote:
> Hi,
>  
> I am new to camel .When I try to call REST service using camel , I am
> getting null as response.I don't know where I am missing.
> *camel-config.xml:*
>  
>  
>  
>  
>  
>  
>  
>  
> *Web.xml:*
>  
> name
> MyCamel
>  
>  
>  
>  
> routeBuilder-MyRoute
>  
> classpath:camel-config.xml
>  
>  
>  
> CamelContextLifecycle
>  
> main.java.org.apache.camel.example.servletlistener.MyLifecycle  
>  
>  
>  
>  
>  
> org.apache.camel.component.servletlistener.JndiCamelServletContextListener  
>  
>  
>  
>  
> CamelServlet
>  
> org.apache.camel.component.servlet.CamelHttpTransportServlet  
> 1
>  
>  
>  
>  
> CamelServlet
> /camel/*
>  
>  
> *MyLifeCycle.java:*
> public class MyLifecycle implements CamelContextLifecycle {
>  
> public void beforeStart(ServletCamelContext camelContext, JndiRegistry
> registry)
> throws Exception {
> registry.bind("pakBean", new TestRest());
> }
> .....
> }
>  
> TestRest.java:
> public class TestRest
>  
> {
> int status = 0;
> String problemDescription = null;
> String Response=null;
> String details = null;
> String
> URL1="http://localhost:8080/rest/services/token/fetchTest?authMethod=Basic&authUsername=test&authPassword=Test";  
> public String test(){
> CamelContext camel = new DefaultCamelContext();
> ProducerTemplate template = camel.createProducerTemplate();
> Exchange exchange = template.request(URL1, new Processor() {
> public void process(Exchange exchange) throws Exception {
> exchange.getIn().setHeader(Exchange.HTTP_URI,URL1);
> exchange.getIn().setHeader(Exchange.HTTP_METHOD,"GET");
> exchange.getIn().setHeader(Exchange.AUTHENTICATION,"Basic");
>  
> exchange.getIn().setHeader(Exchange.CONTENT_TYPE,"application/json");
> exchange.getIn().setBody("Hello");
> String uri = exchange.getIn().getHeader(Exchange.HTTP_URI,
> String.class);
> String out = exchange.getIn().getHeader(Exchange.HTTP_METHOD,
> String.class);
> System.out.println("uri:::"+uri);
> System.out.println("out:::"+out);
> }
>  
> });
> Message out = exchange.getOut();//Getting null and it is not hitting
> the service
>  
> }
>  
>  
> }
>  
> Please help me to fix the issue.Thanks in adv.
>  
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/Calling-REST-service-without-using-spring-tp5762125.html  
> Sent from the Camel - Users mailing list archive at Nabble.com.
>  


Re: Calling REST service without using spring

Posted by Willem Jiang <wi...@gmail.com>.
Hi,

I don’t think you specify the right CamelContextLifecycle value.
It should be “org.apache.camel.example.servletlistener.MyLifecycle" instead of “main.java.org.apache.camel.example.servletlistener.MyLifecycle”

The server should be accessed from "http://localhost:8080/CONTEXT_PATH/camel/getPak”, CONTEXT_PATH is the war file name.

--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.iteye.com (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem



On January 27, 2015 at 12:23:25 AM, chitra1986 (chitra1986@gmail.com) wrote:
> Hi,
>  
> I am new to camel .When I try to call REST service using camel , I am
> getting null as response.I don't know where I am missing.
> *camel-config.xml:*
>  
>  
>  
>  
>  
>  
>  
>  
> *Web.xml:*
>  
> name
> MyCamel
>  
>  
>  
>  
> routeBuilder-MyRoute
>  
> classpath:camel-config.xml
>  
>  
>  
> CamelContextLifecycle
>  
> main.java.org.apache.camel.example.servletlistener.MyLifecycle  
>  
>  
>  
>  
>  
> org.apache.camel.component.servletlistener.JndiCamelServletContextListener  
>  
>  
>  
>  
> CamelServlet
>  
> org.apache.camel.component.servlet.CamelHttpTransportServlet  
> 1
>  
>  
>  
>  
> CamelServlet
> /camel/*
>  
>  
> *MyLifeCycle.java:*
> public class MyLifecycle implements CamelContextLifecycle {
>  
> public void beforeStart(ServletCamelContext camelContext, JndiRegistry
> registry)
> throws Exception {
> registry.bind("pakBean", new TestRest());
> }
> .....
> }
>  
> TestRest.java:
> public class TestRest
>  
> {
> int status = 0;
> String problemDescription = null;
> String Response=null;
> String details = null;
> String
> URL1="http://localhost:8080/rest/services/token/fetchTest?authMethod=Basic&authUsername=test&authPassword=Test";  
> public String test(){
> CamelContext camel = new DefaultCamelContext();
> ProducerTemplate template = camel.createProducerTemplate();
> Exchange exchange = template.request(URL1, new Processor() {
> public void process(Exchange exchange) throws Exception {
> exchange.getIn().setHeader(Exchange.HTTP_URI,URL1);
> exchange.getIn().setHeader(Exchange.HTTP_METHOD,"GET");
> exchange.getIn().setHeader(Exchange.AUTHENTICATION,"Basic");
>  
> exchange.getIn().setHeader(Exchange.CONTENT_TYPE,"application/json");
> exchange.getIn().setBody("Hello");
> String uri = exchange.getIn().getHeader(Exchange.HTTP_URI,
> String.class);
> String out = exchange.getIn().getHeader(Exchange.HTTP_METHOD,
> String.class);
> System.out.println("uri:::"+uri);
> System.out.println("out:::"+out);
> }
>  
> });
> Message out = exchange.getOut();//Getting null and it is not hitting
> the service
>  
> }
>  
>  
> }
>  
> Please help me to fix the issue.Thanks in adv.
>  
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/Calling-REST-service-without-using-spring-tp5762125.html  
> Sent from the Camel - Users mailing list archive at Nabble.com.
>