使用http-client处理器从SpringXD将http请求标头传递给RestAPI [英] Passing http request header to RestAPI from SpringXD using http-client processor

查看:141
本文介绍了使用http-client处理器从SpringXD将http请求标头传递给RestAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从我的http客户端处理器模块中找到一个休息api。
我想知道如何将请求标头发送到我想要的网址。
如果没有请求标题,我会收到500内部服务器错误。

I am trying to hit a rest api from my http-client processor module. I would like to know how can I send request header to the url I am trying to hit. Without the request header I get 500 Internal Server Error.

mapRequestHeaders选项对于传递标题是否有用,请有人举个例子。

Will mappedRequestHeaders option be useful to pass headers, can someone give an example please.

以下是我的流的样子:

jms --destination= | http-client --url='''https://hostname:11210/cards/accounts?eName=John%20Smith&caFSix=426600&caLF=1234''' --httpMethod=GET | log


推荐答案

是的,假设你的标题是 x-foo ,将 http-client 处理器的 mappedRequestHeaders 属性设置为HTTP_REQUEST_HEADERS,x-foo

Yes, assuming your header is x-foo, set the mappedRequestHeaders property of the http-client processor to "HTTP_REQUEST_HEADERS, x-foo".

然后,如果入站JMS消息有一个标题 x-foo = bar ,它将映射到 http-client

Then, if the inbound JMS message has a header x-foo=bar, it will be mapped in the http-client.

这假设您使用的是本地或兔子消息总线;对于redis,你必须配置总线来传递你的标题。

This assumes you are using a local or rabbit message bus; for redis you would have to configure the bus to pass your header.

如果入站JMS消息没有标题,你将需要一个自定义处理器(或自定义 http-client )使用< header-enricher /> 添加标题。

If the inbound JMS message doesn't have the header, you will need a custom processor (or a customized http-client) to use a <header-enricher/> to add the header.

编辑

您可以使用redis,但需要添加标题名称如果你想让它们遍历总线,请使用servers.yml配置redis:

You can use redis, but you need to add your header name to the servers.yml config for redis if you want them to traverse the bus:

xd:
  messagebus:
    redis:
      headers:   

但是如果你直接在你的头文件中添加一个标题自定义http-client,他们不需要通过总线。

However if you add a header-enricher directly in your custom http-client, they won't need to pass over the bus.

您可以使用json路径从JMS消息中提取内容。如果您还需要更改有效负载,您可能会发现使用自定义转换器更容易创建消息。

You can use json path to extract the content from your JMS message. If you also need to change the payload, you may find it easier to use a custom transformer to create the message.

Message<?> transform(Message<String> msg) {

   return MessageBuilder.withPayload(newPayload)
             .copyHeaders(msg)
             .setHeader("accept", "...")
             .setHeader(...)
             . ...
             .build();
}

编辑#2

我刚试过它,它对我来说很好......

I just tested it and it worked fine for me...

<header-enricher input-channel="input" output-channel="toHttp">
    <header name="foo" value="bar" />
</header-enricher>

<channel id="toHttp" />

<int-http:outbound-gateway id='http-client'
    request-channel='toHttp' url-expression="${url}" http-method="${httpMethod}"
    expected-response-type='java.lang.String' charset='${charset}'
    reply-timeout='${replyTimeout}' reply-channel='output'
    mapped-request-headers="${mappedRequestHeaders}"
    mapped-response-headers="${mappedResponseHeaders}">
</int-http:outbound-gateway>

<channel id="output" />
<channel id="input" />

使用此流定义...

xd:>stream create ticktock --definition "time --fixedDelay=5 | http-client --url='''http://localhost:8080/http/receiveGateway''' --mappedRequestHeaders=HTTP_REQUEST_HEADERS,foo | log --expression=#root" --deploy

...带有这些结果...

...with these results...

18:02:20,284  INFO task-scheduler-3 sink.ticktock - GenericMessage [payload=2015-02-02 18:02:20 from the other side, headers={Server=Apache-Coyote/1.1, foo=bar, connection=keep-alive, id=8a444177-b96d-70c3-58e7-d92067d6b18e, Content-Length=39, contentType=text/plain, http_statusCode=200, Date=1422918140000, timestamp=1422918140284}]
18:02:25,292  INFO task-scheduler-3 sink.ticktock - GenericMessage [payload=2015-02-02 18:02:25 from the other side, headers={Server=Apache-Coyote/1.1, foo=bar, connection=keep-alive, id=d62b46ed-dcc7-6dd0-35ea-b7b988c4f2f1, Content-Length=39, contentType=text/plain, http_statusCode=200, Date=1422918145000, timestamp=1422918145292}]

尽可能看, foo = bar 出现在最后的消息中。

As you can see, foo=bar appears in the final message.

现在,在HTTP消息中,默认情况下,用户定义的标题以 X - 为前缀,因此 foo 标题显示为 X- foo:bar 在HTTP中。

Now, in the HTTP message, by default, the user-defined headers get prefixed by X- so the foo header shows up as X-foo: bar in HTTP.

为了取消 X - ,你需要另外调整 http-client ...

In order to suppress the X-, you need another tweak to the http-client...

<header-enricher input-channel="input" output-channel="toHttp">
    <header name="foo" value="bar" />
</header-enricher>

<channel id="toHttp" />

<int-http:outbound-gateway id='http-client'
    request-channel='toHttp' url-expression="${url}" http-method="${httpMethod}"
    expected-response-type='java.lang.String' charset='${charset}'
    reply-timeout='${replyTimeout}' reply-channel='output'
    header-mapper="mapper">
</int-http:outbound-gateway>

<beans:bean id="mapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <beans:property name="userDefinedHeaderPrefix" value="" />
    <beans:property name="outboundHeaderNames" value="${mappedRequestHeaders}" />
    <beans:property name="inboundHeaderNames" value="${mappedResponseHeaders}" />
</beans:bean>

<channel id="output" />
<channel id="input" />

这篇关于使用http-client处理器从SpringXD将http请求标头传递给RestAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆