泽西岛中的 @Context HttpServletRequest 范围
我正在 Glassfish 3.1 服务器上使用 Jersey 构建一个 API,需要访问 HttpServletRequest 对象才能获取某些标头、调用者的 ip 等。我可以将其注入到每个 API 方法调用中,但似乎更有效只需在全球范围内进行即可。像下面的代码片段那样在类级别注入它是否安全,或者这会导致 Glassfish 出现某种并发问题吗?
@Path("/myapi")
@RequestScoped
public class MyApiResource {
@Context private UriInfo context;
@Context private HttpServletRequest request;
I'm building an API using Jersey on the Glassfish 3.1 server and need to get access to the HttpServletRequest object in order to get certain headers, caller's ip, etc. I could just inject it into every API method call but it seems more efficient to just do it globally. Is it safe to inject it at the class level like in the snippet below or will this cause some kind of concurrency issues with Glassfish?
@Path("/myapi")
@RequestScoped
public class MyApiResource {
@Context private UriInfo context;
@Context private HttpServletRequest request;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是安全的。不要使用 @RequestScoped 注释 - JAX-RS 资源默认为请求范围。这意味着为每个请求创建一个新实例,因此不存在并发问题。
It is safe. Don't use the @RequestScoped annotation - JAX-RS resources are request scoped by default. That means a new instance gets created for each request hence no concurrency issues.