问题描述
我们使用的是Resteasy 2,但我们正在升级到Resteasy 3,并且HttpServletRequest
注入始终为null
.
We were using Resteasy 2 but we are upgrading to Resteasy 3 and the HttpServletRequest
injection is always null
.
我们修改后的安全拦截器/过滤器如下所示:
Our modified security interceptor/filter that looks like:
@Provider
@ServerInterceptor
@Precedence("SECURITY")
public class SecurityInterceptor implements ContainerRequestFilter, ContainerResponseFilter {
@Context
private HttpServletRequest servletRequest;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
// Need access to "servletRequest" but it is always null
if (!isTokenValid(pmContext, method)) {
requestContext.abortWith(ACCESS_DENIED);
}
}
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
// post processing
}
}
应用程序类如下:
@ApplicationPath("/")
public class RestApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();
public RestApplication() {
// Interceptors
this.singletons.add(new SecurityInterceptor());
// Services
this.singletons.add(new MyService());
}
public Set<Class<?>> getClasses() {
return this.empty;
}
public Set<Object> getSingletons() {
return this.singletons;
}
}
示例API:
@Path("/test")
public class MyService extends BaseService {
@Context HttpServletRequest servletRequest;
@GET
@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
public Response hello() {
// Need access to HttpServletRequest but it's null
return Response.ok("hello").build();
}
}
但是,请查看此和此帖子,我看不到HttpServletRequest
注入提供程序.
However, looking at this and this posts, I don't see HttpServletRequest
injection provider.
这使我相信我可能需要一个额外的插件.这是已安装的内容:
This leads me to believe that I may need an additional plugin. This is what is installed:
jose-jwt
resteasy-atom-provider
resteasy-cdi
resteasy-crypto
resteasy-jackson2-provider
resteasy-jackson-provider
resteasy-jaxb-provider
resteasy-jaxrs
resteasy-jettison-provider
resteasy-jsapi
resteasy-json-p-provider
resteasy-multipart-provider
resteasy-spring
resteasy-validator-provider-11
resteasy-yaml-provider
有什么想法吗?
推荐答案
基于 @peeskillet 的建议,进行修改以返回新的类实例(而不是单例)解决了我的问题.
Based on @peeskillet suggestion, modifying to return new class instances instead of singletons resolved my issue.
因此,修改后的javax.ws.rs.core.Application
文件如下所示:
Thus my modified javax.ws.rs.core.Application
file looks like:
@ApplicationPath("/")
public class RestApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> classes = new HashSet<Class<?>>();
public RestApplication() {
// Interceptors
this.classes.add(SecurityInterceptor.class);
// Services
this.classes.add(MyService.class);
}
public Set<Class<?>> getClasses() {
return this.classes;
}
public Set<Object> getSingletons() {
return this.singletons;
}
}
这篇关于Resteasy 3 @Context HttpServletRequest始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!