为什么 Tomcat 上运行的 RESTEasy Web 服务会忽略错误页面?

发布于 2024-08-23 13:42:50 字数 304 浏览 3 评论 0原文

我正在使用部署在 Tomcat 上的 RESTEasy 开发 REST-ful Web 服务。我配置了一个错误页面,当请求期间发生任何异常时,该页面会获取异常消息并根据该消息生成 XML。

这对于任何应用程序生成的异常都适用。但是,如果客户端发送无法正确解组的无效 XML,则会引发 javax.xml.bind.UnmarshalException 并使用 Tomcat 的默认错误页面而不是我的错误页面。

我已在 web.xml 中将错误页面配置为错误代码 500。

使用错误页面是使用 RESTEasy 时处理错误的正确方法还是有其他方法?

I'm developing a REST-ful web service using RESTEasy deployed on Tomcat. I've configured an error page which takes the exception's message and generates an XML based on it when any exception occurs during the request.

This works fine for any application generated exceptions. However, if client sends an invalid XML which cannot be unmarshalled correctly, an javax.xml.bind.UnmarshalException is thrown and Tomcat's default error page is used instead of mine.

I have configured my error page to the error-code 500 in web.xml.

Is using error pages the correct way to handle errors when using RESTEasy or is there an alternative way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2024-08-30 13:42:50

最好的方法是使用 ExceptionMapper。您创建一个实现 ExceptionMapper 的类 UnmarshalExceptionMapper。您可以使用“@Provider”对其进行注释,并在应用程序构造函数中执行“classes.add(UnmarshalExceptionMapper.class)”或“singletons.add(new UnmarshalExceptionMapper())”。

您的异常映射器将如下所示:

 @provider
 public class UnmarshalExceptionMapper 
            implements ExceptionMapper<UnmarshalException> {
    public Response toResponse(UnmarshalException exception) {
        ResponseBuilder rb =
            Response.status(
                  Response.Status.BAD_REQUEST)  // Most appropriate HTTP status code
            .entity(your xml goes here)
            .type("application/xml");  
        return rb.build();
   }
 }

请注意,您必须将类型设置为“application/xml”,因为当前未针对异常映射器完成内容协商。要进行自己的内容协商,请从请求中获取 HttpHeaders,找到“accept”标头,并相应地设置类型。

The best way is to use an ExceptionMapper. You create a class UnmarshalExceptionMapper that implements ExceptionMapper. You annotate this with "@Provider" and in your Application constructor you do "classes.add(UnmarshalExceptionMapper.class)" or "singletons.add(new UnmarshalExceptionMapper())".

Your exception mapper will look something like this:

 @provider
 public class UnmarshalExceptionMapper 
            implements ExceptionMapper<UnmarshalException> {
    public Response toResponse(UnmarshalException exception) {
        ResponseBuilder rb =
            Response.status(
                  Response.Status.BAD_REQUEST)  // Most appropriate HTTP status code
            .entity(your xml goes here)
            .type("application/xml");  
        return rb.build();
   }
 }

Note that you must set the type to "application/xml" because currently content negotiation is NOT done for exception mappers. To do your own content negotiation, get the HttpHeaders from the request, find the "accept" header, and set the type accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文