创建过滤器以处理异常JERSEY

问题描述 投票:0回答:1

我需要创建一个处理异常的过滤器。详细说明我的程序中使用泽西过滤器时出现异常并且必须返回500错误。这是我的Filter.class:

@Provider
public class Filter implements ContainerResponseFilter {


    @Override
    public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException {


    }
}

如何指定抛出异常时调用此方法并返回500代码错误?有人可以帮帮我吗?

jersey jersey-2.0 jersey-client
1个回答
0
投票

您可以扩展ExceptionMapper:

@Provider
public class ApplicationExceptionMapper implements ExceptionMapper<Exception> {

    @Override
    public Response toResponse(Exception ex) { 
       return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                    .type("text/plain").build();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.