HTTP组件的自定义HTTPBinding

时间:2015-08-14 09:04:14

标签: apache-camel

如果HTTP响应代码不是200或201,我试图抛出异常。我试图覆盖http4.DefaultHttpBinding,但是类永远不会被调用。我在这里做错了什么?

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&amp;httpClient.socketTimeout=${http.notificationTimeout}" id="http4.1.to"/>

1 个答案:

答案 0 :(得分:0)

如果http response code is 300+,绑定将在调用doWriteResponse(...)之前抛出异常。尝试覆盖自定义绑定中的writeResponse(...),并验证是否使用了绑定。