简体   繁体   English

非新泽西州JAX-RS自定义异常和状态码

[英]Non-Jersey JAX-RS Custom Exception with Status Code

I'm using Apache Wink with JAX-RS, and I'm working on throwing custom exceptions for error handling. 我正在将Apache Wink与JAX-RS一起使用,并且正在引发自定义异常以进行错误处理。 I was able to get something working far enough to send back a custom JSON message with a status code, but I'd like the ability to use this custom Exception for more than just one type of Response (in this case, Status.BAD_REQUEST ). 我能够获得足够的工作来发送带有状态码的自定义JSON消息,但是我希望能够对多种类型的Response(在本例中为Status.BAD_REQUEST )使用此自定义Exception。 。 How can I inject a status code into my exception so that I can throw more than one type of status with the custom error message? 如何将状态代码注入异常中,以便可以通过自定义错误消息抛出不止一种状态?

JAX-RS Resource JAX-RS资源

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response memberRegistration(
        JSONObject json,
        @HeaderParam("Client-ID") String clientId,
        @HeaderParam("Client-Secret") String clientSecret) throws WebApplicationException, Exception {

        // Authenticate the user using the credentials provided
        appAuth.checkClientId(clientId, clientSecret);
        if (!"new".equals(json.get("type_of_membership"))) {
            throw new WebApplicationException("Type of membership (new) is not correct for this route.");
        }
        return Response.ok(json, MediaType.APPLICATION_JSON).build();

}

Custom Exception 自订例外

public class WebApplicationException extends Exception implements Serializable
{
    private static final long serialVersionUID = 1L;
    public WebApplicationException() {
        super();
    }
    public WebApplicationException(String msg) {
        super(msg);
    }
    public WebApplicationException(String msg, Exception e)  {
        super(msg, e);
    }
}

Custom Exception Handler/ExceptionMapper implementation 自定义异常处理程序/ ExceptionMapper实现

@Provider
public class WebApplicationExceptionHandler implements ExceptionMapper<WebApplicationException> 
{
    @Override
    public Response toResponse(WebApplicationException exception) 
    {
        JSONObject json = new JSONObject();
        json.put("message", exception.getMessage());
        return Response.status(Status.BAD_REQUEST).entity(json.toString()).build();
    }
}

I don't know if I got your question, but I could suggest that adding an instance variable to your custom exception to hold the status code that you want, you can do that as follow: 我不知道是否有您的问题,但是我建议您将一个实例变量添加到自定义异常中,以保存所需的状态代码,您可以按照以下步骤进行操作:

public class WebApplicationException extends Exception implements Serializable
{
private static final long serialVersionUID = 1L;
private Response.Status status = Response.Status.BAD_REQUEST;

public WebApplicationException() {
    super();
}
public WebApplicationException(String msg) {
    super(msg);
}
public WebApplicationException(String msg, Exception e)  {
    super(msg, e);
}

public WebApplicationException(String msg, Response.Status status) {
    super(msg);
    this.status = status;
}
public WebApplicationException(String msg, Exception e, Response.Status status)  {
    super(msg, e);
    this.status = status;
}

public Response.Status getStatus() {
     return status;
}
}

then you can modify the return statement in "WebApplicationExceptionHandler" as 那么您可以将“ WebApplicationExceptionHandler”中的return语句修改为

return Response.status(exception.getStatus()).entity(json.toString()).build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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