2016-09-27 219 views
0

我有一个骆驼路由配置为从JMS队列读取并将其发布到服务。 我的路线是:使用Camel发出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. 

什么,我做错了什么?我是否需要将邮件内容设置为我的发布请求的正文?

+0

A 400意味着错误的请求。所以这意味着您的请求消息或您的URI是错误的。服务器日志是否显示您的请求已发送并且看起来正确?关于服务器收到的标题呢? –

+0

我无法访问服务器日志。正如我所提到的,我从exchange.in获得的请求对象为null。 –

+0

这是来自服务器的响应,如果你的意思是400错误的请求?最简单的可能是设置自己的本地服务器并首先进行测试。通过这种方式,您可以在进行集成测试之前准确查看接收到的标题以及机构的外观。这里是一个简单的服务器玩http://tinyserver.sourceforge.net/ –

回答

0

这不会是String.class

in.getBody(String.class); 

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

应当HttpServletResponse

HttpServletRequest request = in.getBody(HttpServletRequest.class) 
+0

它可以是字符串类型。请检查这一点。 http://camel.apache.org/using-getin-or-getout-methods-on-exchange.html。我可以通过将其转换为字符串来打印响应。 –

+0

也为第二部分,这个stackoverflow问题指出如何使用httprequest对象.. http://stackoverflow.com/questions/19976812/how-to-dump-http-body-and-headers-sent-with-http-部件与 - Apache的骆驼 –