从一个 Web 应用程序抛出 WebapplicationException 并在另一个 Web 应用程序上获取 UniformInterfaceException

发布于 2024-11-28 03:25:02 字数 838 浏览 0 评论 0原文

我看到一个奇怪的问题。我有两个网络应用程序。其中之一是我们使用 Jersey 公开的其余 Web 服务。另一个具有 JSF 前端实现,它调用上述 Web 服务来获取详细信息。我们使用 Tomcat 作为容器。

我面临的问题是,当我调用休息Web服务时,我像这样抛出WebApplicationException

catch (CustomExceptions e) {
    ResponseBuilder response = new ResponseBuilderImpl();
    response.status(500);
    response.entity(e.getStackTrace());
    throw new WebApplicationException(e, response.build()); 
}

另一方面,在FE Web应用程序上,我执行以下操作:

try {
    r.get(MyClass.class);
    return "SUCCESS";
} catch (WebApplicationException wae) {
    return "FAILURE";
} catch (UniformInterfaceException wae) {
    return "FAILURE";
}

在catch块中,我期待WebApplicationException,但它抛出UniformInterfaceException,这很奇怪。此外,如果它抛出 UniformInterfaceException,它不会维护堆栈跟踪。甚至我从休息呼叫中传递的响应也丢失了。有人可以帮助我如何从其余调用中获取原始堆栈跟踪吗?

提前致谢。

I see a weird issue. I have two webapps. One for the rest webservice that we exposed using Jersey. Another has the JSF Front End implementation which calls above webservice to fetch the details. We are using Tomcat as a container.

The issue that i am facing is when i call a rest webservice, i throw WebApplicationException like this

catch (CustomExceptions e) {
    ResponseBuilder response = new ResponseBuilderImpl();
    response.status(500);
    response.entity(e.getStackTrace());
    throw new WebApplicationException(e, response.build()); 
}

And on the other hand on FE webapps, i do the following:

try {
    r.get(MyClass.class);
    return "SUCCESS";
} catch (WebApplicationException wae) {
    return "FAILURE";
} catch (UniformInterfaceException wae) {
    return "FAILURE";
}

Here in the catch block i was expecting the WebApplicationException but its throwing UniformInterfaceException which is weird. Also if it throws UniformInterfaceException, It does not maintain the stacktrace. Even the response that i passed in from rest call is lost. Can somebody help me how can i get the original stacktrace from the rest call?

Thanks in advance.

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

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

发布评论

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

评论(2

晨曦÷微暖 2024-12-05 03:25:02

正如用户指南api 文档,当您尝试检索一个 UniformInterfaceException 时,预期结果是按类型指定实体,但收到非 200 系列响应。 WebApplicationException 是一个方便的异常,供您在服务器端使用以在异常中嵌入自定义响应。

没有什么是“丢失”的。请记住,您正在此处发出 HTTP 请求。如果您想自己检查状态代码和响应,请使用

ClientResponse response = r.get(ClientResponse.class);
if (response.getStatus() == <expected status code>) [
    response.getEntity(MyClass.class);
    // do happy path stuff
} else {
    // bad stuff here
}

Alternately,假设一切都会顺利,并且仅检查异常的响应:

try {
    r.get(MyClass.class);
} catch (UniformInterfaceException e) {
    ClientResponse response = e.getResponse();
    // do whatever with the raw response
}

As specified in both the user guide and the api documentation, a UniformInterfaceException is the expected result when you attempt to retrieve a specific entity by type but receive a non-200-series response. The WebApplicationException is a convenience exception for your use on the server side to embed custom responses in an exception.

Nothing is "lost". Keep in mind you're making an HTTP request here. If you want to inspect the status code and response yourself, use

ClientResponse response = r.get(ClientResponse.class);
if (response.getStatus() == <expected status code>) [
    response.getEntity(MyClass.class);
    // do happy path stuff
} else {
    // bad stuff here
}

Alternately, assume everything will go well, and only check the response on exceptions:

try {
    r.get(MyClass.class);
} catch (UniformInterfaceException e) {
    ClientResponse response = e.getResponse();
    // do whatever with the raw response
}
死开点丶别碍眼 2024-12-05 03:25:02

我之前也遇到过同样的问题。我认为这是因为 Jersey WebResource.get 方法仅抛出 UniformInterfaceException ,它不在 WebApplicationExcpetion 的继承层次结构中。我所做的就是放弃 Jersey 客户端 api,而是使用普通的 ApacheHttpClient。将能够在 httpResponse 内容中获得正确的堆栈跟踪。

希望这有帮助。


谢谢,瑞安。我尝试了你的解决方案。很不错。

I had same issue before. I think it's because Jersey WebResource.get method throws only UniformInterfaceException which is not in the inheritance hierarchy of WebApplicationExcpetion. What I did is to abandon Jersey client side api but use plain ApacheHttpClient. Will be able to get the right stacktrace in the httpResponse contents.

Hope this helps.


Thanks, Ryan. I tried your solution. Very nice.

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