Call 1 jersey resource class from another Jersey resource class with @Context ServletContext

巧了我就是萌 提交于 2019-12-09 16:39:35

问题


I have Jersey resource class A calling a method in resource class B.Both classes have a @Context ServletContext servletContext at the class level. When I instantiate class B to call it from resource class A using its empty constructor, servletContext is null in the class B method being called. Is there any Jersey framework way I can call class B and yet have the servletContext retain its values/attributes from class A.


回答1:


You can instantiate class B using ResourceContext. I.e. in class A you can have:

@Context private ResourceContext rc;

And then in you can instantiate resource B as follows:

B resourceB = rc.getResource(B.class);

See ResourceContext javadoc for more info.



来源:https://stackoverflow.com/questions/6944500/call-1-jersey-resource-class-from-another-jersey-resource-class-with-context-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!