Java 骆驼:如何检查响应 http 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23983681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Camel: how to check response http response
提问by Surasin Tancharoen
I am pretty new with Camel. I have been trying to submit a data (Json from a file) to a webservice. This is my code:
我对骆驼很陌生。我一直在尝试向网络服务提交数据(来自文件的 Json)。这是我的代码:
public static void main(String args[]) throws Exception {
// create CamelContext
CamelContext context = new DefaultCamelContext();
// add our route to the CamelContext
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("file:data/inbox?noop=true")
.marshal()
.string()
.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
.to("http://www.a-service.com");
}
});
// start the route and let it do its work
context.start();
Thread.sleep(10000);
// stop the CamelContext
context.stop();
}
Then the webservice will response with Json which can be {result:OK} or {result:FAIL}
然后网络服务将使用 Json 响应,它可以是 {result:OK} 或 {result:FAIL}
Now, if a response has responseCode as 200, Camel will consider as success.
现在,如果响应的 responseCode 为 200,Camel 将视为成功。
My question is, how can I have a validating process for responsed JSon so that if it is FAIL, Camel should not consider as success?
我的问题是,我怎样才能对响应的 JSon 进行验证过程,以便如果它失败,Camel 不应视为成功?
SolutionCredit @Namphibian:
解决方案信用@Namphibian:
By adding processor and the end. This code has been tested:
通过添加处理器和结束。此代码已经过测试:
from("file:data/inbox?noop=true")
.marshal()
.string("UTF-8")
.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
.to("http://monrif-test.userspike.com/monrif/rss/monrif_-all-global")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
String msg = in.getBody(String.class);
System.out.println("Response: " + msg);
if(msg.contains("OK")){
// go to party
}else{
throw new Exception("test exception");
}
}
});
采纳答案by Namphibian
There are two broad strategies you can use to achieve this.
您可以使用两种广泛的策略来实现这一目标。
Processor Based:
基于处理器:
Add a processor to the end of the route. In this processor do the check if the webservice then responds with a true or false value.
在路由的末尾添加一个处理器。在此处理器中检查 Web 服务是否以 true 或 false 值响应。
A processor would look something like this:
处理器看起来像这样:
package com.example;
import java.util.Map;
import org.apache.camel.Body;
import org.apache.camel.Exchange;
import org.apache.camel.Handler;
import org.apache.camel.Headers;
import org.apache.camel.Message;
public class GeneralProcessor {
@Handler
public void PrepapreErrorImportReport
(
@Headers Map hdr
, Exchange exch
)
{
//todo: Get the message as a string;
Message in = exch.getIn();
String msg = (String)in.getBody();
// Now check if body contains failed or ok.
if(msg.contains("OK")){
//todo: go party the message was OK
}
else{
//todo: Oh Oh! Houston we have a problem
}
}
}
You can then modify your route to use this processor.
然后您可以修改您的路线以使用此处理器。
The Simple Expression Language
简单表达式语言
This is one way the other way is to use the simple expression language. See the example below on how to use this.
这是一种方式,另一种方式是使用简单的表达式语言。请参阅下面的示例了解如何使用它。
from("file:data/inbox?noop=true")
.marshal()
.string()
.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
.to("http://www.a-service.com")
.choice()
.when(simple("${body} contains 'OK'")).to("activemq:okqueue")
.otherwise().to("activemq:queue:other");
Notice the simple("${body} contains 'OK'")
piece of code. That is the power of simple.
注意simple("${body} contains 'OK'")
这段代码。这就是简单的力量。
Both approaches have uses.
这两种方法都有用。
回答by BaskarNatarajan
In the Process
method , you can use below method and it will work
在该Process
方法中,您可以使用以下方法,它将起作用
LOGGER.info("Response code " + message.getHeader(exchange.HTTP_RESPONSE_CODE, Integer.class));