java - 使用 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
解决方案
小心。在 Camel 路由中,Camel 消息正文通常成为传出消息的正文。这可能是 Camel 无论如何都使用 POST 的原因:您的 HTTP 请求有一个正文。null
在发送 HTTP 请求之前尝试将 Camel 消息正文设置为。
不确定这是否正确,我总是使用 Java 路由:
<setBody>
<simple>${bodyAs(null)}</simple>
</setBody>
但是,我不知道为什么查询字符串不起作用。
推荐阅读
- java - Java 方法参数给出编译错误“Unexpected bound”
- asp.net - 垃圾回收正在进行时无法分析托管堆
- python-3.x - 我将如何使用来自两个域的 cookie?
- javascript - 如何为显示图像的内联元素编写 HTML 代码以在网页中使用?
- angular7 - ngFor中的点击事件不起作用Angular 7
- c++ - 使用#define 在 C++/g++ 中包含文件
- html - 为什么我在角度反应形式的 *ngIf 中出现错误
- swift - 如何在今天的扩展中添加图表框架
- python - 为什么我会收到此错误以及如何解决它
- c# - 使用 httpclient 异步任务下载 http 源