Spring Integration - 如何使用http outbound-gateway发送POST参数 [英] Spring Integration - how to send POST parameters with http outbound-gateway
问题描述
我正在尝试使用Spring Integration和http outbound-gateway组合一个非常简单的HTTP POST示例。
我需要能够发送带有一些POST参数的HTTP POST消息,如我会用 curl
:
I'm trying to put together a really simple HTTP POST example using Spring Integration and a http outbound-gateway.
I need to be able to send a HTTP POST message with some POST parameters, as I would with curl
:
$ curl -d 'fName=Fred&sName=Bloggs' http://localhost
我可以让它工作(没有POST参数)如果我发送一个简单的 String
作为接口方法的参数,但我需要发送一个pojo,其中pojo的每个属性都成为POST参数。
I can get it working (without the POST parameters) if I send a simple String
as the argument to the interface method, but I need to send a pojo, where each property of the pojo becomes a POST parameter.
我有以下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
}
我的班级全力以赴如下:
And my class to kick it all off looks like this:
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);
}
}
当我运行上述内容时,我得到:
When I run the above, I get:
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:如何传递帖子请求参数到http-outbound 但是这是一个稍微不同的用例,因为OP试图发送他的pojo的JSON表示,我不是,答案是关于设置标题,而不是POST参数。
I've googled a lot for this, but cannot find any examples of sending HTTP POST parameters with an outbound-gateway (I can find lots about setting HTTP Headers, but that's not what I'm trying to do here)
The only thing I did find was spring-integration: how to pass post request parameters to http-outbound but it's a slightly different use case as the OP was trying to send a JSON representation of his pojo which I am not, and the answer talks about setting headers, not POST parameters.
任何帮助p非常感谢;
谢谢
Nathan
Any help with this would be very much appreciated;
Thanks
Nathan
推荐答案
感谢@ jra077关于 Content-Type
的指示,这就是我解决它的方法。
Thanks to the pointers from @jra077 regarding Content-Type
, this is how I solved it.
我的SI配置现在看起来像这样 - 重要的是添加 Content-Type
标题:
My SI config now looks like this - the important bit was adding the 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>
然后我更改了界面以获取地图
因为它是参数而不是pojo:
Then I changed my interface to take a Map
as it's argument rather than the pojo:
public interface RequestGateway {
String echo(Map<String, String> request);
}
pojo本身仍然像以前一样;并且更改了调用服务的类,以便创建 Map
并传递它:
The pojo itself remains as before; and the class that invokes the service is changed so that it creates a Map
and passes it:
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
,但暂时回答了我的问题。
I'm sure there are several more elegant ways of transforming the pojo into a Map
, but for the time being this answers my question.
发送POST参数一个http出站网关,您需要将 Content-Type
设置为 application / x-www-form-urlencoded
和你需要传递一个 Map
的键/值对。
To send POST parameters with a http outbound-gateway you need to set the Content-Type
to application/x-www-form-urlencoded
and you need to pass a Map
of key/values pairs.
这篇关于Spring Integration - 如何使用http outbound-gateway发送POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!