Spring XD http-client处理器发送text / plain而不是application / json

时间:2015-06-08 16:48:18

标签: json spring-xd

我正在尝试创建一个发送POST请求作为流的一部分的流。

stream create form-time --definition "<working source> | generate-form-transformer: transform --script=form-generation.groovy  --outputType=application/json | http-client --inputType=application/json ..." --deploy

它可以工作,但它将contentType设置为text / plain而不是application / json,因为我的服务器不支持,所以它会引发400。 (虽然内容是完美的JSON)。

变换器返回一个XD元组,因此据我所知,从Tuple到JSON的转换应该正确完成(事实上,如前所述,有效负载是正确的)。

但是,对于http-client,它不会将 contentType 修改为text/plain;UTF-8而不是application/json

有什么建议吗?我错过了什么吗?谢谢。

2 个答案:

答案 0 :(得分:2)

我不相信--inputType--outputType次转化应设置标头,他们只是执行转换;一种技术是在http-client ...

中添加一个标题
$ git diff
diff --git a/modules/processor/http-client/config/http-client.xml b/modules/processor/http-client/config/http-client.xml
index cf7d2e2..c2ecd00 100644
--- a/modules/processor/http-client/config/http-client.xml
+++ b/modules/processor/http-client/config/http-client.xml
@@ -10,7 +10,7 @@
                        http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">

        <int-http:outbound-gateway id='http-client'
-               request-channel='input' url-expression="${url}" http-method="${httpMethod}"
+               request-channel='inputX' url-expression="${url}" http-method="${httpMethod}"
                expected-response-type='java.lang.String' charset='${charset}'
                reply-timeout='${replyTimeout}' reply-channel='output'
                mapped-request-headers="${mappedRequestHeaders}"
@@ -19,5 +19,9 @@

        <channel id="output" />
        <channel id="input" />
+       <channel id="inputX" />
+       <header-enricher input-channel="input" output-channel="inputX">
+               <header name="content-type" value="application/json" />
+       </header-enricher>

现在spring-xd-modules中有一个包含头的处理器模块。

答案 1 :(得分:0)

  

stream create form-time --definition&#34; | generate-form-transformer:transform --script = form-generation.groovy --outputType = application / json | http-client --inputType = application / json ...&#34; --deploy

您是否为--outputType=application/json模块设置了http-client。另外,--inputType中的http-client不是必需的,因为前一个模块的输出类型已经是application\json。您可能打算将其设置为--outputType