Apache Camel and CXF : How do i send HTTP status code from bean

11,027

Solution 1

Here is a workaround, i dont know if this is the only way to do it, but this works for me. Change the return type of your method Foo to the Response(javax.ws.rs.core) and then wrap your object ("instance of myobject") in this response and also you can specify the mime type if you want to .. following is a sample..

public class Bar {
    public Response foo(String request, Exchange exchange){
        //make instance of MyObject
        MyObject myObj = new myObj();
        //do your processing and set the object in response code
        Response response = Response.status(Status.ACCEPTED).entity(myObj).type(MediaType.APPLICATION_JSON).build();
        return response;
    }
}

Solution 2

After you set the HTTP_RESPONSE_CODE you need to tell the exchange an error occured.

exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 202);
exchange.getOut().setFault(true);

Solution 3

Have you tired something like this in your route

Response r = Response.status(202).entity("Created").build();
exchange.getOut().setBody(r);

Since you want to change the default response you need to override the exchanges out message

Share:
11,027
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a restful web service based on cxf and camel. I am using Camel routing for my workflow it is something like this..

    Client --> My Rest Service Class --> Camel Custom Processors --> some method Foo of Bean Bar

    The bean FooBar looks like something this ..

    public class Bar {
    
        public MyObject foo(String request, Exchange exchange){
        //do some procesing 
    
        return instance of MyObject;
    }
    

    Now the problem is that whenever i test this i get a respone code of 200 at client side. Now if i want this code to be something else than 200 i need to set it in HttpServletResponse or some other javax.ws.rs.core.Response object but how i do i access this response object.

    I tried the following but it didn't solve my problem.

    1. exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 202);

    2. exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 202);

    3. exchange.setProperty(Exchange.HTTP_RESPONSE_CODE, 202);

    4. Response.status(202).build();