首页 > 解决方案 > 使用 Camel 中的查询字符串对 REST Web 服务进行 HTTP GET 调用

问题描述

我需要使用查询字符串对 REST WS 进行 GET 调用,uri 必须是这样的:

http://somehost.com/someservice?parm1=value

我需要分配身体参数“parm1”的值(带有getter和setter的普通pojo),所以这是骆驼路线:

<setHeader headerName = "Exchange.HTTP_QUERY" id = "queryStringSomeService">
    <simple>parm1=${body.someField}</simple>
</setHeader>
<setHeader headerName = "CamelHttpMethod" id = "httpMethodSomeService">
    <constant>GET</constant>
</setHeader>
<to id="SOME_SERVICE" uri="http4:/somehost.com/someservice?bridgeEndpoint=true" />

问题是查询字符串从未添加到 uri 中,http 方法最终使用了它的 POST,尽管我添加了 header 以显式设置 GET。

我使用的是 Spring DSL。

在文档的页面http://camel.apache.org/http4.html中,建立了选择HTTP方式的规则;在“使用 GET 或 POST 调用”部分中,但显然它们没有在这种情况下应用。

更新:

将主体设置为 null 可以解决将 HTTP 方法更改为 GET 的问题,尽管在文档中说您使用 CamelHttpMethod 标头覆盖它。

但是对于查询字符串,我已经尝试了所有变体;使用 Exchange.HTTP_URI 和 Exchange.HTTP_QUERY,都没有工作

这是 WS 调用之前的交换标头:

Accept: application/json
CamelHttpCharacterEncoding: ISO-8859-1
CamelHttpMethod: GET
CamelHttpQuery: ?parm1=value
CamelHttpResponseCode: 200
CamelHttpResponseText: OK
CamelHttpUri: /someservice
CamelHttpUrl: http://somehost/someservice
CamelRedelivered: false
CamelRedeliveryCounter: 0
Connection: close
Content-Type: application/json;charset=UTF-8
Date: Fri, 14 Sep 2018 16:08:19 GMT
Last-Modified: Thu, 13 Sep 2018 13:33:30 GMT
Set-Cookie: JSESSIONID=YXQkEAUAjh0yWsu4UYwSG8vE.5aa71417-9e93-3be1-99ca-7b4ec1d6f2a0; Path=/ca_tar_tarjeta
Transfer-Encoding: chunked
breadcrumbId: ID-wildfly01-1536931422750-9-23

标签: javaapache-camel

解决方案


小心。在 Camel 路由中,Camel 消息正文通常成为传出消息的正文。这可能是 Camel 无论如何都使用 POST 的原因:您的 HTTP 请求有一个正文。null在发送 HTTP 请求之前尝试将 Camel 消息正文设置为。

不确定这是否正确,我总是使用 Java 路由:

<setBody>
    <simple>${bodyAs(null)}</simple>
</setBody>

但是,我不知道为什么查询字符串不起作用。


推荐阅读