For 400 bad request in cxfrs:rsclient in camel, the exchange is null. For 200 http status, the exchange object is set

722 Views Asked by At

Below is the code snippet to consume an api endpoint. For 200 http response, the exchange object contains the payload received. But for 400 response, the payload received is not set in exchange object. Is anything missing in the code below?

Exchange exchange = serviceProducer.send(endPoint, new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.setPattern(ExchangePattern.InOut);
                Message inMessage = exchange.getIn();
                inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.TRUE);
                inMessage.setHeader(Exchange.CONTENT_TYPE, "application/json");
                inMessage.setHeader(Exchange.HTTP_METHOD, "POST");
                inMessage.setHeader(Exchange.HTTP_QUERY, "clientId=" + ClientId);
                inMessage.setBody(request);
                inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, SearchResponse.class);
                inMessage.setHeader(Exchange.CONTENT_TYPE, "application/json");
            }
        });
SearchResponse searchResponse = (SearchResponse) exchange.getOut().getBody();
1

There are 1 best solutions below

2
Sneharghya Pathak On

getOut() creates a blank output message. You need to use getIn() or getMessage().

SearchResponse searchResponse = (SearchResponse) exchange.getIn().getBody();

https://camel.apache.org/manual/latest/faq/using-getin-or-getout-methods-on-exchange.html#UsinggetInorgetOutmethodsonExchange-UsinggetInorgetOutmethodsonExchange