本文介绍了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 响应code 是 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!