Spring 集成 - 如何使用 http outbound-gateway 发送 POST 参数

Spring Integration - how to send POST parameters with http outbound-gateway

我正在尝试使用 Spring 集成和 http outbound-gateway.
组合一个非常简单的 HTTP POST 示例 我需要能够发送带有一些 POST 参数的 HTTP POST 消息,就像我使用 curl:

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

如果我发送一个简单的 String 作为接口方法的参数,我可以让它工作(没有 POST 参数),但我需要发送一个 pojo,其中每个 [= pojo 的 61=] 成为 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 class 看起来像这样:

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

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

    .... getters and setters
}

我的 class 开始这一切看起来像这样:

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]

为此我在谷歌上搜索了很多,但找不到任何使用 outbound-gateway 发送 HTTP POST 参数的示例(我可以找到很多关于设置 HTTP Headers,但是这不是我在这里要做的)
我唯一找到的是 但它的用例略有不同,因为 OP 试图发送他的 pojo 的 JSON 表示,而我不是,答案是关于设置 headers,不是 POST 参数。

如有任何帮助,我们将不胜感激;
谢谢
内森

感谢 @jra077 关于 Content-Type 的指点,我就是这样解决的。

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

<?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 本身保持原样;并且调用该服务的 class 已更改,以便它创建一个 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 outbound-gateway 发送 POST 参数,您需要将 Content-Type 设置为 application/x-www-form-urlencoded 并且您需要传递一个 Map key/values对.