Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(@Context HttpServletResponse response not work in resteasy

Tags:

java

servlets

web

Web application with resteasy framework.

@Path("/do3")
@GET
public void response(@Context HttpServletResponse response) throws IOException{ 
    response.setStatus(202);

}

why get /do3 return 204, not 202? Thanks in advance.

PS: (1) I switch to @post method. it also can't get expected code: 202 by get. (2) response.addHeader("key", "value"); can work normally.

like image 543
jiafu Avatar asked Apr 16 '12 01:04

jiafu


1 Answers

RESTEasy is interpreting your method as best it can - you didn't specify a return type so it returns a 204 (No Content) back to the client. A void GET method really does not make alot of sense and should be avoided, or converted into another HTTP verb (like POST).

On topic, this is not the right way to set the status of responses from JAX-RS calls. You should use the ResponseBuilder instead.

Response.status(202).build();

You can of course, use the injected HttpServletResponse for any other thing as long as it makes sense within the context of the call:

response.setHeader("Location", "http://www.example.com/myresource/5");
like image 137
Perception Avatar answered Sep 28 '22 06:09

Perception