响应中的JAX / Jersey自定义错误代码

时间:2010-01-07 16:52:52

标签: java http jersey response

在泽西岛,我们如何“替换”与已知状态代码相关联的状态字符串?

e.g。

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

生成包含以下内容的HTTP响应:

HTTP/1.1 401 Unauthorized

我(不是我,但客户端应用程序)希望将响应视为:

HTTP/1.1 401 Authorization Required

我尝试了以下方法,但徒劳无功:

1)这只是在HTTP响应正文中添加了字符串

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

2)同样的结果:

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

感谢您的帮助!

-spd

2 个答案:

答案 0 :(得分:38)

要在Jersey中执行此操作,您将拥有WebApplicationException类的概念。一种方法是简单地扩展此类和所有方法以设置返回的错误文本。在你的情况下,这将是:

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());
     }

}

现在在你的代码中实现了rest服务,你只需要抛出这种类型的新异常,传入构造函数中的文本值,例如。

throw new UnauthorizedException("Authorization Required");

这可以为每个Web异常创建一个这样的类,并以类似的方式抛出。

在泽西用户指南中也对此进行了解释 - 尽管代码实际上有点不正确:

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

答案 1 :(得分:4)

我不确定JSR 339: JAX-RS 2.0: The Java API for RESTful Web Services已经涵盖了这个问题。

您可能需要为此扩展Response.StatusType

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;
}

以下是一些扩展的statust类型。

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);
    }
}

这就是我的方式。

@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.