Spring集成 - 如何使用http outbound-gateway

时间:2015-05-28 07:51:47

标签: java spring http post spring-integration

我正在尝试使用Spring Integration和http出站网关组建一个非常简单的HTTP POST示例 我需要能够发送带有一些POST参数的HTTP POST消息,就像curl一样:

$ curl -d 'fName=Fred&sName=Bloggs' http://localhost

如果我将一个简单的String作为参数发送到接口方法,我可以使它工作(没有POST参数),但我需要发送一个pojo,其中pojo的每个属性都成为POST参数

我有以下SI配置:

<?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:int-http="http://www.springframework.org/schema/integration/http"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">

    <int:gateway id="requestGateway"
             service-interface="RequestGateway"
             default-request-channel="requestChannel"/>

    <int:channel id="requestChannel"/>

    <int-http:outbound-gateway request-channel="requestChannel"
                           url="http://localhost"
                           http-method="POST"
                           expected-response-type="java.lang.String"/>

</beans>

我的RequestGateway界面如下所示:

public interface RequestGateway {
    String echo(Pojo request);
}

我的Pojo课程如下:

public class Pojo {
    private String fName;
    private String sName;

    public Pojo(String fName, String sName) {
        this.fName = fName;
        this.sName = sName;
    }

    .... getters and setters
}

我的课程全力以赴如下:

public class HttpClientDemo {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("/si-email-context.xml");
        RequestGateway requestGateway = context.getBean("requestGateway", RequestGateway.class);

        Pojo pojo = new Pojo("Fred", "Bloggs");
        String reply = requestGateway.echo(pojo);
        System.out.println("Replied with: " + reply);
    }
}

当我运行上述内容时,我得到:

org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [Pojo] and content type [application/x-java-serialized-object]

我已经搜索了很多内容,但找不到任何使用出站网关发送HTTP POST参数的示例(我可以找到很多关于设置HTTP标头的内容,但这不是我在这里要做的)<登记/> 我唯一找到的是spring-integration: how to pass post request parameters to http-outbound,但它是一个稍微不同的用例,因为OP试图发送他的pojo的JSON表示,我不是,答案是关于设置标题,而不是POST参数。< / p>

非常感谢任何帮助; 由于

1 个答案:

答案 0 :(得分:2)

感谢@ jra077关于Content-Type的指示,这就是我解决它的方法。

我的SI配置现在看起来像这样 - 重要的是添加Content-Type标题:

<?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:int-http="http://www.springframework.org/schema/integration/http"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">

    <int:gateway id="requestGateway"
             service-interface="RequestGateway"
             default-request-channel="requestChannel">
        <int:method name="sendConfirmationEmail">
            <int:header name="Content-Type" value="application/x-www-form-urlencoded"/>
        </int:method>
    </int:gateway>

    <int:channel id="requestChannel"/>

    <int-http:outbound-gateway request-channel="requestChannel"
                           url="http://localhost"
                           http-method="POST"
                           expected-response-type="java.lang.String"/>

</beans>

然后我改变了我的界面,以Map作为它的参数,而不是pojo:

public interface RequestGateway {
    String echo(Map<String, String> request);
}

pojo本身仍然像以前一样;并且更改了调用服务的类,以便创建Map并传递它:

public class HttpClientDemo {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("/si-email-context.xml");
        RequestGateway requestGateway = context.getBean("requestGateway", RequestGateway.class);

        Pojo pojo = new Pojo("Fred", "Bloggs");

        Map<String, String> requestMap = new HashMap<String, String>();
        requestMap.put("fName", pojo.getFName());
        requestMap.put("sName", pojo.getSName());

        String reply = requestGateway.echo(requestMap);
        System.out.println("Replied with: " + reply);
    }
}

我确信有几种更优雅的方法可以将pojo转换为Map,但暂时还是回答了我的问题。

要使用http出站网关发送POST参数,您需要将Content-Type设置为application/x-www-form-urlencoded,并且需要传递Map个键/值对。