Spring Integration – 如何使用http outbound-gateway发送POST参数

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

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

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

我有以下SI配置:

       

我的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 } 

而我的全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] 

我已经搜索了很多,但找不到任何使用出站网关发送HTTP POST参数的例子(我可以找到很多关于设置HTTP标头的内容,但这不是我在这里尝试做的)
我唯一发现的是spring-integration:如何将post请求参数传递给http-outbound,但是这是一个稍微不同的用例,因为OP试图发送他的pojo的JSON表示,我不是,并且答案是关于设置标题,而不是POST参数。

对此有任何帮助将非常感谢;
谢谢
弥敦道

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

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

           

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

 public interface RequestGateway { String echo(Map 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 requestMap = new HashMap(); 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