English 中文(简体)
JAX/Jersey Custom error code in Response
原标题:

In Jersey, how can we replace the status string associated with a known status code?

e.g.

return Response.status(401).build();

generates a HTTP response that contains:

HTTP/1.1 401 Unauthorized

I (not me, but the client application) would like to see the response as:

HTTP/1.1 401 Authorization Required

I tried the following approaches but in vain:

1) This just adds the String in the body of the HTTP response

return Response.status(401).entity("Authorization Required").build();

2) Same result with this too:

ResponseBuilder rb = Response.status(401);
rb = rb.tag("Authorization Required");
return rb.build();

Appreciate your help!

-spd

问题回答

To do this in Jersey you have the concept of WebApplicationException class. One method is to simply extend this class and all one of the methods to set the error text that is returned. In your case this would be:

import javax.ws.rs.*;
import javax.ws.rs.core.*;
import javax.ws.rs.core.Response.*;


public class UnauthorizedException extends WebApplicationException {

    /**
      * Create a HTTP 401 (Unauthorized) exception.
     */
     public UnauthorizedException() {
         super(Response.status(Status.UNAUTHORIZED).build());
     }

     /**
      * Create a HTTP 404 (Not Found) exception.
      * @param message the String that is the entity of the 404 response.
      */
     public UnauthorizedException(String message) {
         super(Response.status(Status.UNAUTHORIZED).entity(message).type("text/plain").build());
     }

}

Now in your code that implements the rest service you would simply throw a new exception of this type, passing in the text value in the constructor e.g.

throw new UnauthorizedException("Authorization Required");

That can create a class like this for each of your web exceptions and throw in a similar fashion.

This is also explained in the Jersey user guide - although the code is actually slightly incorrect:

https://jersey.github.io/nonav/documentation/latest/user-guide.html/#d4e435

I m not sure JSR 339: JAX-RS 2.0: The Java API for RESTful Web Services already covered this or not.

You might have to extend the Response.StatusType for this.

public abstract class AbstractStatusType implements StatusType {

    public AbstractStatusType(final Family family, final int statusCode,
                              final String reasonPhrase) {
        super();

        this.family = family;
        this.statusCode = statusCode;
        this.reasonPhrase = reasonPhrase;
    }

    protected AbstractStatusType(final Status status,
                                 final String reasonPhrase) {
        this(status.getFamily(), status.getStatusCode(), reasonPhrase);
    }

    @Override
    public Family getFamily() { return family; }

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

    @Override
    public int getStatusCode() { return statusCode; }

    public ResponseBuilder responseBuilder() { return Response.status(this); }

    public Response build() { return responseBuilder().build(); }

    public WebApplicationException except() {
        return new WebApplicationException(build());
    }

    private final Family family;
    private final int statusCode;
    private final String reasonPhrase;
}

And here are some extended statust types.

public class BadRequest400 extends AbstractStatusType {

    public BadRequest400(final String reasonPhrase) {
        super(Status.BAD_REQUEST, reasonPhrase);
    }
}

public class NotFound404 extends AbstractStatusType {

    public NotFound404(final String reasonPhrase) {
        super(Status.NOT_FOUND, reasonPhrase);
    }
}

This is how I do.

@POST
public Response create(final MyEntity entity) {

    throw new BadRequest400("bad ass").except();
}

@GET
public MyEntity read(@QueryParam("id") final long id) {

    throw new NotFound404("ass ignorant").except();
}

// Disclaimer
// I m not a native English speaker.
// I don t know what  bad ass  or  ass ignorant  means.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签