简体   繁体   中英

JAX-RS and Jersey: SEVERE: Mapped exception to response: 501

We've got a JAX-RS resource method that handles GET requests for our resource. That GET method is currently unimplemented and we'd like to return a response code that says so.

We originally were using the following technique to generate a 501 response:

Response.status(501)

This yields SEVERE warnings in our logs (SEVERE: Mapped exception to response: 501), something the QA team and our dev manager frown upon.

Here's what we're doing now (that avoids the warning):

public class JaxrsResponseStatus501NotImplemented implements StatusType {

public Family getFamily() {
    return Family.SERVER_ERROR;
}

public String getReasonPhrase() {
    return "Not Implemented";
}

public int getStatusCode() {
    return 501;
}

}

And this in the JAX-RS method:

public ResourceDto getResource(@PathParam("resource_id") String resourceId) {
    throw new WebApplicationException(Response.status(new JaxrsResponseStatus501NotImplemented()).entity(ERROR_MESSAGE_GET_NOT_IMPLEMENTED).build());
}

My questions:

1) Why isn't Response.Status.NOT_IMPLEMENTED defined? Most of the other typical responses are defined as constants by the library but I don't see 501 pre-defined in the 1.8 version I'm using.

I do see that it is defined in ClientResponse.Status but I was under the impression that the com.sun.jersey.api.client package hierarchy was for client-side development. I don't even think I'm pulling that jar in right now.

2) What's the correct way to emit a 501 response? In the absence of a Response.Status.NOT_IMPLEMENTED (or any other response that does not have pre-defined constant) is the method above (a custom exception implementing StatusType) the correct way to go?

3) When you implement a StatusType (assuming you need to), how do you know what "Family" to associate the exception with? What are the Family associations used for? Where are they used?

Thanks!

Personally, I'd just use Response.status(501) and move on with life.

Update: Okay, well I'm not sure where that SEVERE is coming from. I don't know that I've ever seen it, but if you dig down into that status method, you'll find where it would hit code like this:

default: {
    return new StatusType() {
        @Override
        public int getStatusCode() {
            return statusCode;
        }

        @Override
        public Family getFamily() {
            return toFamilyCode(statusCode);
        }

        @Override
        public String getReasonPhrase() {
            return "";
        }
    };
}

and toFamilyCode() is

public static Family toFamilyCode(final int statusCode) {
    switch(statusCode / 100) {
        case 1: return Family.INFORMATIONAL;
        case 2: return Family.SUCCESSFUL;
        case 3: return Family.REDIRECTION;
        case 4: return Family.CLIENT_ERROR;
        case 5: return Family.SERVER_ERROR;
        default: return Family.OTHER;
    }
}

That should give you some guidance.

Apparently, creating a custom exception is necessary to avoid the SEVERE log warning. The one unanswered question I had was what to use for the Family code in that custom exception for a 501 error.

A quick check of the Jersey client-side code (which, curiously, does define a 501 status response) shows they determine the status code like so:

       Status(final int statusCode, final String reasonPhrase) {
        this.code = statusCode;
        this.reason = reasonPhrase;
        switch(code/100) {
            case 1: this.family = Family.INFORMATIONAL; break;
            case 2: this.family = Family.SUCCESSFUL; break;
            case 3: this.family = Family.REDIRECTION; break;
            case 4: this.family = Family.CLIENT_ERROR; break;
            case 5: this.family = Family.SERVER_ERROR; break;
            default: this.family = Family.OTHER; break;
        }
    }

That puts any 500-series errors in the Family.SERVER_ERROR category. Just fYI

All. I feel very dumb.

The solution I posted that creates a custom Exception works great. But it's completely unnecessary and doesn't solve the problem (a SEVERE warning in the logs).

After spending some time with the Jersey source (which I should have done in the first place) it turns out that any exception with a response-code 500 or higher will emit that SEVERE exception. Doesn't matter how you go about putting that status-code into the exception.

Very sorry for the red herring.

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