简体   繁体   中英

HTTPServletResponse status code is not in HTTP response header

I have pretty simple JAX-RS WS:

@Stateless
@Path("/foo")
public class FooFacadeBean {     

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String performFooCall(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        response.setStatus(500);
        return "Hello";
    }
}

After deployment I execute: curl -v localhost:8080/foo, the result was:

About to connect() to localhost port 8080
Trying 127.0.0.1...
connected
Connected to localhost (127.0.0.1) port 8080
GET /foo HTTP/1.1
User-Agent: curl/7.26.0
Host: localhost:8080
Accept: */

HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2 Java/Sun Microsystems Inc./1.6)
Server: GlassFish Server Open Source Edition 3.1.2
Content-Type: text/html
Transfer-Encoding: chunked
Date: Thu, 26 Jul 2012 13:52:10 GMT

Hello 

Connection to host localhost left intact
Closing connection

As you can see status code in HTTP hasn't changed at all, despite of it was set manually. Why does this happen? I've spent a lot of hours googling without any results.

Such workaround as returning Response object, something like this:

Response.status(404).entity("Hello").build();

works perfectly!

Glassfish uses JERSEY as JAX-RS implementation. I use embedded variant of Glassfish.

Basically, that's not how you should use Jersey.

Here's what's happening:

  • The method executes, and you interact with the ServletResponse and set the status code to 500. But since you don't write any data, the response isn't committed.

  • Then the method returns a String value. Since Jersey has no way of knowing if you've interacted with the ServletResponse or not, it behaves normally.

  • A String method that returns implies a 200 response code (void implies 204, etc). Jersey tries to set the response code to the default, ultimately by calling setStatus(200) on the response instance. Since the response hasn't been committed yet, this isn't a problem, and the response of 500 is changed to 200.

  • The HttpServletResponse is committed and is sent back to the client.

I'm assuming that what you want to do is return a different status code, but in an exceptional case only. The way to do that is to return a String in the normal case, and then throw a WebApplicationException in the exceptional case.

@GET
@Produces(MediaType.TEXT_HTML)
public String performFooCall() {
    if (isNormal()) {
        return "Hello";
    }
    throw new WebApplicationException(Response.status(500).entity("Hello").build());
}

Alternatively, you can throw a standard exception, and control how it is rendered as a response with a separate ExceptionMapper .

Jersey building a new response so setting the response code in your code, is override later. You have to build a new response in your code and put you string in the response (and change your return value for that), or you can create a filter that will work later on in the filter chaing and change the response status.

look here for more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM