resteasy 中的全局自定义异常处理程序 [英] Global custom exception handler in resteasy

查看:47
本文介绍了resteasy 中的全局自定义异常处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为所有意外错误创建全局异常处理程序.因为不可能像这样创建所有可能的类:

Is it possible to make global exception handler for all unexpected errors. Because it's impossible to make all possible classes like this:

public class ExceptionHandler implements ExceptionMapper<JsonMappingException> {...}

我想要这样的东西:

public class ExceptionHandler implements ExceptionMapper<Exception> 

推荐答案

您可能可以执行以下操作:

You could probably do the following:

@Provider
public class GlobalExceptionHandler implements ExceptionMapper<Exception> {
    public Response toResponse(Exception exception) {
        return Response.status(Status.INTERNAL_SERVER_ERROR)
            .entity("An error occured").type(MediaType.TEXT_PLAIN)
            .build();
    }
}

我刚刚在 RESTEasy 3.0.6.Final 中对其进行了测试,它似乎可以工作.请记住,500 Internal Server Error 状态代码可能并不总是合适的.

I have just tested it in RESTEasy 3.0.6.Final and it seems to work. Keep in mind that the 500 Internal Server Error status code may not always be appropriate.

这篇关于resteasy 中的全局自定义异常处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆