Announcement Announcement Module
Collapse
No announcement yet.
Spring integration inbound http gateway form post Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring integration inbound http gateway form post

    Hi All,

    I've been wrestling with this for a while and I am not making any progress.

    I've integrated with a 3rd party API which takes a form post and I am achieving this with spring integration.
    The following segment works as expected

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:http="http://www.springframework.org/schema/integration/http"
           xmlns:int="http://www.springframework.org/schema/integration"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    			http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    			http://www.springframework.org/schema/integration
    			http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
                http://www.springframework.org/schema/integration/http
                http://www.springframework.org/schema/integration/http/spring-integration-http-2.2.xsd">
    
        <int:channel id="postChannel" />
    
        <http:outbound-gateway url="{postUrl}" request-channel="postChannel" http-method="POST"
                               expected-response-type="java.lang.String"
                               reply-timeout="10000" charset="UTF-8">
            <http:uri-variable name="postUrl" expression="headers['X-CUSTOM-HTTP-REQUEST-URL']" />
        </http:outbound-gateway>
    
        <bean id="postMessagingOperations" class="org.springframework.integration.core.MessagingTemplate">
            <constructor-arg ref="postChannel"/>
        </bean>
    
    </beans>

    Code:
        
    public String sendMessage(final Map<String, String> message) {
    
    
            Message<Map<String, String>> mailRequest = MessageBuilder.withPayload(message)
                    .setHeader("X-CUSTOM-HTTP-REQUEST-URL", url)
                    .setHeader(CONTENT_TYPE_HEADER, "application/x-www-form-urlencoded")
                    .setHeader(AUTHORIZATION_HEADER, encodeUsernameAndPassword(API,
                            applicationProperties.getApiKey())).build();
    
    
            Message<String> response = (Message<String>) messagingOperations.sendAndReceive(mailRequest);
        }
    I have a suite of automated tests and I want to mock out this interaction so I want to do the exact inverse of the fragment above.

    I am using the following fragment and I am consistently getting "405 (Method Not Allowed)"

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:int="http://www.springframework.org/schema/integration"
           xmlns:http="http://www.springframework.org/schema/integration/http"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    			http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    			http://www.springframework.org/schema/integration
    			http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
    			http://www.springframework.org/schema/integration/http
    			http://www.springframework.org/schema/integration/http/spring-integration-http-2.2.xsd">
    
        <int:channel id="eventChannel" />
        <http:inbound-gateway request-channel="eventChannel"
                              id="eventGateway" name="/event" path="/event" supported-methods="POST"
                              request-payload-type="java.util.Map"  />
    
    
        <int:service-activator input-channel="eventChannel" ref="eventEndPoint" method="handleEvent"/>
    
    </beans>
    Code:
    @MessageEndpoint
    public class EventEndPoint {
    
        public void handleEvent(Map<String, String> eventMessage) {
           // do something
        }
    
    }
    I am doing something wrong I am sure but I just can't see it. Any ideas?

    Thanks,
    MD

  • #2
    Hi!

    First of all try to remove request-payload-type="java.util.Map" on inbound part and if won't work again turn on DEBUG for org.springframewrok category and try to find where "405 (Method Not Allowed)" is coming from.

    If you Content-Type HTTP header is really "application/x-www-form-urlencoded" the <http:inbound-gateway> produces the Message with MultiValueMap payload. Otherwise there is no built-in converter for plain Map.

    Take care,
    Artem

    Comment


    • #3
      Hi Artem,

      Thanks for the quick reply. Spot on! Removed the Map reference and it certainly helped.

      I discovered that the root issue was that I was not thinking and just trying to do the inverse pattern.

      The issue was that I included the inbound-gateway in my application context. I'd set up my outbound-gateway integration with the 3rd party in my application context and that works fine. Following the same pattern for an inverse setup I put inbound gateway in the application context of the mock server as well.

      Logs told the story when my app tried to hit my mock the dispatcher servlet of the mock would complain that the path "/event" was not mapped. Moved the inbound gateway into the servlet context everything works.

      ahhh... happy testing.

      Thanks again,

      MD

      Comment

      Working...
      X