HTTP组件的自定义HTTPBinding [英] Custom HTTPBinding for HTTP Component
本文介绍了HTTP组件的自定义HTTPBinding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果HTTP响应代码不是200或201,我将尝试引发异常.我尝试覆盖http4.DefaultHttpBinding,但从未调用该类.我在这里做什么错了?
I'm trying to throw an exception if HTTP response code is anything other than 200 or 201. I tried to override http4.DefaultHttpBinding, but the class is never getting called. What am I doing wrong here?
public class CustomHttpBinding extends org.apache.camel.component.http4.DefaultHttpBinding {
Logger log = Logger.getLogger(CustomHttpBinding.class);
private static final Integer HTTP_OK = 200;
private static final Integer HTTP_CREATED = 201;
@Override
public void doWriteResponse(Message message, HttpServletResponse response, Exchange exchange) throws IOException {
Integer httpResponseCode = message.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
if(!httpResponseCode.equals(HTTP_OK) && !httpResponseCode.equals(HTTP_CREATED) ) {
throw new IOException("HTTP error code for HLR response is " + httpResponseCode);
}
else {
super.doWriteResponse(message, response, exchange);
}
}
}
在xml中:
<bean id="customHttpBinding" class="com.test.response.CustomHttpBinding" />
<endpoint uri="http4:${http.url.1}?httpBinding=#customHttpBinding&httpClient.socketTimeout=${http.notificationTimeout}" id="http4.1.to"/>
推荐答案
如果 http响应代码是300+ ,则绑定将在调用doWriteResponse(...)
之前引发异常.尝试在自定义绑定中覆盖writeResponse(...)
,并验证是否使用了绑定.
If the http response code is 300+, the binding will throw exception before it calls doWriteResponse(...)
. Try overriding writeResponse(...)
in your custom binding, and verify that your binding is used.
这篇关于HTTP组件的自定义HTTPBinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文