Custom HTTPBinding for HTTP Component

孤街醉人 提交于 2019-12-24 13:45:17

问题


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);
    }
 }
}

In xml:

<bean id="customHttpBinding" class="com.test.response.CustomHttpBinding" />

<endpoint uri="http4:${http.url.1}?httpBinding=#customHttpBinding&amp;httpClient.socketTimeout=${http.notificationTimeout}" id="http4.1.to"/>

回答1:


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.



来源:https://stackoverflow.com/questions/32006284/custom-httpbinding-for-http-component

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!