Results 1 to 6 of 6

Thread: Rest Webservice using Spring Integration HTTP inbound gateway

Hybrid View

  1. #1
    Join Date
    Nov 2011
    Posts
    13

    Default Rest Webservice using Spring Integration HTTP inbound gateway

    Hi,

    I am trying to expose a rest webservice using HTTP inbound gateway. I am not able to access the "retailer" path variable in the service activator.

    I am using Spring 3.0.6 and Spring integration 2.0.5 RELEASE.

    Please help, I tried googling but I am not able to find any suitable sample which will give me this pathvariable access.

    I have created following things.

    1. Inbound Channel
    2. Outbound Channel
    3. Inbound Gateway
    4. Service Activator


    Following is the configuration I have created.

    WEB.xml

    Code:
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    	<servlet>
    		<servlet-name>businessIntegration</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/spring/servlet-context.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>businessIntegration</servlet-name>
    		<url-pattern>/*</url-pattern>
    	</servlet-mapping>
    </web-app>
    servlet-context.xml
    Code:
    	<bean name="auditLogGateway" class="uk.co.fresca.api.common.auditlog.AuditLogGateway"></bean>
    
    	<!-- findAll Log -->
    	<int:channel id="findAllLogsInboundChannel" />
    	<int-http:inbound-gateway request-channel="findAllLogsInboundChannel" name="/findLogs/{retailer}/{product}"  mapped-request-headers="Accept-Charset:utf-8" supported-methods="GET" reply-channel="findAllLogsTransformedChannel"/>
    	<int:service-activator expression="@auditLogGateway.fetchAllLogs(#pathVariables.retailer)" id="findLogs" input-channel="findAllLogsInboundChannel" output-channel="findAllLogsOutboundChannel" >
    	</int:service-activator>
    	<int:channel id="findAllLogsOutboundChannel"></int:channel>
    	<int:object-to-json-transformer input-channel="findAllLogsOutboundChannel" output-channel="findAllLogsTransformedChannel"></int:object-to-json-transformer>
    	<int:channel id="findAllLogsTransformedChannel"></int:channel>
    Code:
    	public Message<List> fetchAllLogs(String retailer){
    		System.out.println(retailer);
    		Message<List> retMessage = new GenericMessage<List>(new ArrayList());
    		return retMessage;
    	}

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,853

    Default

    The support for path variables in a Spring Integration HTTP *inbound* adapter is actually new in version 2.1 (which is approaching RC1 very soon). The outbound adapters supported *setting* path vars in 2.0.x, but to grab values from the inbound URI, you will need to use 2.1.

    Here's what you can do in 2.1:

    1. use "path" instead of "name" as an attribute on the adapter and rely on the same path var syntax that you have already.
    2. add a "payload-expression" attribute OR 1 or more <header/> subelements with "expression" attributes themselves
    3. within the expressions, grab any path var values as such: #pathVariables.foo

    Hope that helps.
    -Mark

  3. #3
    Join Date
    Nov 2011
    Posts
    13

    Default

    Thanks Mark,

    This helped a lot. I am trying the same example with 2.1, I will come back with some good news.

    Thanks,

    Amit Solankurkar

  4. #4
    Join Date
    Nov 2011
    Posts
    13

    Default

    Hi Mark,

    Thanks a lot!!

    I have changed the libraries to 2.1.0.M3 and it is working. I am setting the pathVariables in the header and getting them from header in service activator.

    Following is the configuration changed, posting for other's use.


    servlet-config.xml

    Code:
    	<bean name="controller" class="uk.co.fresca.HomeController"></bean>
    
    	<int:channel id="inCh"></int:channel>
    	<int:channel id="outCh"></int:channel>
    	<int-http:inbound-gateway request-channel="inCh" reply-channel="outCh" name="/findlogs/{retailer}/{product}" path="/findlogs/{retailer}/{product}" mapped-request-headers="accept-charset:utf-8">
    		<int-http:header name="retailer" expression="#pathVariables.retailer"/>
    	</int-http:inbound-gateway>
    	<int:service-activator input-channel="inCh" output-channel="outCh" ref="controller" method="process">
    	</int:service-activator>
    In service activator retailer value can be taken from the header.


    One small help, not on priority,

    how to pass this param as a payload expression. I tried changing the payload-expression of the inbound gateway in many ways but not successful.

  5. #5
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,853

    Default

    Are you saying you tried payload-expression="#pathVariables.retailer"?

    That should work, but maybe you need to ensure that the request-payload-type is configured.

  6. #6
    Join Date
    Nov 2011
    Posts
    13

    Default

    Hi Mark,

    I tried payload-expression="#pathVariables.retailer" and it is working fine, as I have a input message type if String.

    public String process(<Message<String> retailer){
    System.out.println(retailer); -- This is printing the retailer value correctly.
    }

    but if I want to use multiple pathvariables then I am planning to use a custom request payload to which I can assign the values. Is there any way to do it?

    Thanks again for the prompt response.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •