简体   繁体   English

从JSF托管Bean调用JAX-RS Web服务

[英]Calling JAX-RS Web Service from JSF managed bean

I currently develop a Web application that can be accessed over HTTP by an Android application or a Web browser. 我目前正在开发一个Web应用程序,Android应用程序或Web浏览器可以通过HTTP访问该Web应用程序。 Both user agents basically provide the same functionalities. 两个用户代理基本上提供相同的功能。 The Android app calls RESTful Web Services built with JAX-RS/Jersey, while Web pages are Facelets backed by JSF managed beans. Android应用程序调用使用JAX-RS / Jersey构建的RESTful Web服务,而Web页面是由JSF托管bean支持的Facelets。 I thus consider that there are two types of possible entry points to the Web app. 因此,我认为Web应用程序有两种可能的入口点。 The Web services do all the necessary work (accessing the resources, performing the database operations within DAOs, etc.) and most importantly, they must act on their own. Web服务完成所有必要的工作(访问资源,在DAO中执行数据库操作等),最重要的是,它们必须自己采取行动。 Therefore, they constitute an independent layer. 因此,它们构成一个独立的层。

In order to reuse the code, is it a good practice to call a Web service from a managed bean? 为了重用代码,从托管bean调用Web服务是一种好习惯吗? Are their life cycle compatible? 他们的生命周期兼容吗?

The idea is to inject the Resource object into the managed bean (with CDI, but not necessarily) and to call its methods programmatically . 这个想法是将Resource对象注入到托管bean中(使用CDI,但不一定),并以编程方式调用其方法。 The Web service would act as a business delegate (?) to the underlying services. Web服务将充当基础服务的业务委托(?)。

I widely googled the question but I didn't get a clear answer. 我在问题上进行了广泛的搜索,但没有得到明确的答案。 I saw some pieces of code where the managed bean invokes a Web service with its URL, but since all my components are located in a single server app, I don't see what prevents me from linking them directly . 我看到了一些代码片段,其中托管Bean使用其URL调用Web服务,但是由于我的所有组件都位于单个服务器应用程序中,因此看不到是什么阻止了我直接链接它们。

Subsidiary question on error handling: I am also annoyed with the idea of catching WebApplicationExceptions from the Web service into my managed bean for returning error messages back to the view (with FacesMessage). 有关错误处理的子问题:我也很讨厌将WebApplicationExceptions从Web服务捕获到我的托管bean中,以便将错误消息返回到视图(使用FacesMessage)。 My father always told me that I should never catch runtime exceptions...So, is there a good way to handle them correctly? 我父亲总是告诉我,我永远都不应捕获运行时异常...因此,有没有一种正确处理它们的好方法?

In this case, you would be better off having the business logic out from the Web Service into "some central shared code". 在这种情况下,最好将业务逻辑从Web Service发送到“某些中央共享代码”中。 Then, then web services and the managed beans would call into the shared code. 然后,Web服务和托管Bean将调用共享代码。

I've put the term "some central shared code" in quotes, as what you use depends on your runtime environment. 我将“某些中央共享代码”用引号引起来,因为您使用的内容取决于运行时环境。 If you're using a JavaEE container like Glassfish or JBoss, this sounds like a perfect use of EJB and stateless session beans (which are designed to ensure the resources are managed well). 如果您使用的是诸如Glassfish或JBoss之类的JavaEE容器,那么这听起来像是对EJB和无状态会话Bean(旨在确保对资源的良好管理)的完美使用。 You could also use Spring Beans as the shared code and centralize the logic that way. 您还可以将Spring Beans用作共享代码,并以此方式集中逻辑。

It all depends on what you find more comfortable from a development and production sense. 这一切都取决于您从开发和生产的角度上觉得更舒适。 But both support injection, so both the Web Service and JSF Managed Beans could have the services injected as any other resource. 但是两者都支持注入,因此Web Service和JSF Managed Bean都可以将服务作为其他任何资源注入。

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

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