问题描述
我在Spring上使用Jersey 1.13.我有一个像这样定义的ContextResolver:
I'm using Jersey 1.13 with Spring. I've got a ContextResolver defined like so:
@Provider
public class ThemeSourceContextResolver implements ContextResolver<ThemeSource> {
@Context private HttpServletRequest request;
@Override
public ThemeSource getContext(Class<?> type) {
return new DefaultThemeSource(request);
}
}
<bean id="themeSourceContextResolver" scope="singleton" class="com.example.ThemeSourceContextResolver" />
以上有效吗?具体来说,在ContextResolver中使用@Context private HttpServletRequest request
是否合法"(或有意义)?由于ContextResolver是一个单例,Jersey/JAX-RS是否执行某些线程本地代理魔术或其他操作以使其可以访问每个请求的HttpServletRequest?
Is the above valid? Specifically, is it "legal" (or does it make sense) to use the @Context private HttpServletRequest request
in a ContextResolver? Since the ContextResolver is a singleton, does Jersey/JAX-RS do some threadlocal proxy magic or something to allow it to have access to the HttpServletRequest of every request?
推荐答案
要为Jersey 2.14更新此答案:
To update this answer for Jersey 2.14:
现在的答案是有时".泽西确实确实为特定的@Context变量(即HttpHeaders,Request,UriInfo和SecurityContext)做代理魔术. 不不支持您的特定情况HttpServletRequest.
The answer is now "sometimes". Jersey does indeed do proxy magic for specific @Context variables, namely HttpHeaders, Request, UriInfo and SecurityContext. Your specific case, HttpServletRequest, is not supported.
请参见 https://jersey.java.net/documentation/latest /jaxrs-resources.html#d0e2578 .
这篇关于您可以在JAX-RS的单例ContextResolver中使用请求范围内的@Context变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!