使用Camel发出POST请求

时间:2016-09-27 09:26:47

标签: java post apache-camel

我有一个camel路由,配置为从JMS队列读取并将其POST到服务。 我的路线是:

from("jms:queue")
.marshal()
.json(JsonLibrary.GSON)
.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.process(new Processor1())
.to("https4:xxxxxx?throwExceptionOnFailure=false")
.process(new MyProcessor())

我的配置:

HttpComponent httpc = getContext().getComponent("http4",HttpComponent.class);
httpc.setHttpConfiguaration(customHttpConfig())

并在customHttpConfig中设置我的身份验证详细信息。

我从服务器收到400错误。但是我可以从Postman api中击中服务器并获得成功的响应。

在我的Processor1类中(在发出请求之前),我能够打印消息的正文,其中包含我的对象的json表示。

但是在POST请求之后的处理器中,我正在执行此操作并获得以下响应:

Message in = exchange.getIn();
in.getBody(String.class); // print the error code 400
HttpServletRequest request = in.getBody(HttpServletRequest.class)// This is null.

我做错了什么?我是否需要将消息内容设置为我的帖子请求的正文?

1 个答案:

答案 0 :(得分:0)

不会是String.class

in.getBody(String.class);

首先检查服务器响应的类型,然后尝试打印或记录交换体。

应为HttpServletResponse

HttpServletRequest request = in.getBody(HttpServletRequest.class)