Apache Camel and CXF : How do i send HTTP status code from bean
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
Admin
Updated on June 04, 2022Comments
-
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 otherjavax.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.
exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 202);
exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 202);
exchange.setProperty(Exchange.HTTP_RESPONSE_CODE, 202);
Response.status(202).build();