CommonMan CommonMan - 1 year ago 76
reST (reStructuredText) Question

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

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
or some other
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();

Answer Source

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( 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;