spring-bootapache-camel

Apache camel - Return type must be void when there is only a single parameter of the type org.apache.camel.Exchange


I am using apache camel 4.3.0 with spring boot 3.2.1 and java 17. I have a route (rest dsl) which accepts an xml and invokes a bean processor method which then makes a database call and returns the data with 200 OK.

As per apache camel's documentation for bean-binding, it says by default, the return value from the bean method is set in the outbound message body. But it also says that the return type of the bean method must be void when it has a single method parameter that is of type Exchange.

Link to camel documentation


@Autowired
DataProcessor dataProcessorBean;

//Rest Route Builder
rest("/api/retrieveWrapper/")
            .id("retrieveWrapper")
            .post("/getData")
            .to("direct:getDataWrapperFromDb");
        
        from("direct:getDataWrapperFromDb")
            .routeId("getDataWrapper")
            .bean(dataProcessorBean, "getWrapper")
            .log("Successfully retrieved data, Size is : ${body.size()}')")
            .end()
            .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(HttpStatus.SC_OK));

//DataProcessor.java
@Component
public class DataProcessor{

    @Autowired
    DataWrapperDao dao;
    
    /*Return type is not void as instructed by camel documentation but has single exchange parameter*/

    public List<Wrapper> getWrapper(Exchange exchange) throws Exception /
    {
        List<Wrapper> dataList = dao.getDataWrapper();//Get data from database
        return dataList;        
    }
}

But my above code still works and sets the list response to the body of the exchange even when the method return type is not void and it has a single parameter of type Exchange. I can log the size of the returned body from the bean method (which contains a list) back in the route successfully. Why is this behavior different from the documentation?


Solution

  • Yeah that is some old requirement that is no longer the case for a modern Camel today. So we can remove that from the docs. The doc should say that input Exchange and output Exchange is not allowed

            public Exchange doSomething(Exchange exchange) {
                // this method should not be called
                return exchange;
            }