我有一个部署在Java Web应用程序中的Javabean,该应用程序基于CMN,BPMN和DMN等复杂的工作流框架。这里的一点是,我的Javabean是从BPMN调用的,在我看来,就像在REST API端点的背景下,因为我可以从Postman中测试已部署的Javabean。当我从BPMN调用Javabean时,我没有看到ServletContext或JSP或请求对象或类似的任何参考。
我需要修改Javabean来添加一些功能,这些功能需要从Web根路径下的数据文件夹加载JSON数据文件。
我确实使用Dynamic Java Web应用程序和Javabean成功构建了一个示例Web应用程序,并能够从请求对象访问Web根路径,并且我能够按预期加载JSON数据文件。
我现在需要从真正的Java Web应用程序框架中进行同样的操作。我面临的唯一问题是,如果我无法访问httpservlet和其他相关对象,则如何访问Web根路径。
我在示例Web应用程序中使用了下面的API,它们成功工作:
Request.getsession (). Getservletcontext (). Getrealpath ();
Servletconfig.getservletcontext (). Getrealpath ("/");
有关示例项目,请参阅下面的相关回购,以阐明以下问题:
https://github.com/tarekahf/javabeanexample2/tree/master
在上述回购中显示的项目中,我正在从Webroot下的文件夹中加载一个JSON文件。我之所以能够这样做,是因为我可以访问请求
对象和 httpservletContext
。请参阅上面存储库中的以下文件中的源代码:
- src \ com \ app \ app \ appleeeclass.java
- webcontent \ data \ list.json
- webcontent \ index.jsp
现在问题现在如何访问ServletContext,如果我看不到它在主Java应用程序中的任何地方定义?
如果从REST API执行线程的上下文中调用Javabean,我如何访问此Javabean中的Webroot路径,以便我可以在root路径下加载所需的JSON数据文件?
塔雷克
I have a JavaBean that is deployed in a Java web application which is based on a complex workflow framework such as CMN, BPMN, and DMN. The point here is that my JavaBean is invoked from a BPMN and it seems to me like it is in a context of a rest API endpoint because I can test the deployed JavaBean from postman. When I invoke the JavaBean from the BPMN, I don't see any reference to ServletContext or JSP or request object or anything like that.
I need to modify the JavaBean to add some feature that requires loading json data file from a data folder under the web root path.
I did build a sample web application successfully using dynamic Java web application and JavaBean and was able to access the web root path from the request object, and I was able to load the json data file as intended.
I need now to do the same from the real Java web application framework. The only issue I'm facing is how to access the web root path if i don't have access to HttpServlet and the other related objects.
I used the APIs below in a sample web application and they worked successfully:
Request.getsession (). Getservletcontext (). Getrealpath ();
Servletconfig.getservletcontext (). Getrealpath ("/");
See the related repo below for the sample project to clarify the question:
https://github.com/tarekahf/JavaBeanExample2/tree/master
In the project presented in the above repo, I'm loading a JSON file from a folder under the webroot. I was able to do so because I have access to the request
object and HttpServletContext
. See the source code in the following files in the above repo:
- src\com\app\EmployeeClass.java
- WebContent\data\list.json
- WebContent\index.jsp
The question now is how to access the ServletContext if I don't see it defined anywhere in the main java application?
If the JavaBean is invoked from the context of a rest API execution thread, how I can access the webroot path in this JavaBean so that I can load the needed JSON data file under the root path?
Tarek
发布评论
评论(1)
如果Javabean是CDI管理的,则可以将
ServletContext
注入以下方式
: https://stackoverflow.com/questions/15749192/how-do--i-load-a-file-file-from-rom-rom-rom-rom-rom-rom-rom-rom-rom-rom-------------
=
“ 是基于弹簧的,然后使用
@Autowired
注释或实现ServletContexTaware接口,将Servlet注入bean中的适当方法是。有关详细信息,请参阅此信息:此外,这是如何在Java中读取文件的另一种资源:
https://www.baeldung.com/reading-file-file-in-in-java
If the JavaBean is CDI managed, you can inject the
ServletContext
with:——
If you just want to read a json file in the java code, I would say that you maybe look at this: read ressource from folder
Edit:
Since it is found that the application is based on Spring, then the appropriate method to inject a servlet into a Bean is by using the
@Autowired
annotation or implementing the ServletContextAware interface. See this for details:https://roytuts.com/how-to-get-servletcontext-and-servletconfig-object-in-a-spring-bean/
Also, this is another resource for how to read files in Java:
https://www.baeldung.com/reading-file-in-java