[英]Inject HttpServletRequest into rest service
I want ot inject HttpServletRequest into my service via @Context
annotation. 我想通过
@Context
注释将HttpServletRequest注入我的服务。 Here is my example: 这是我的示例:
@Path("/")
public class MyService {
@Context
private HttpServletRequest request;
}
I use Apache CXF implementation and here are my dependencies from pom.xml: 我使用Apache CXF实现,这是来自pom.xml的依赖项:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.1.7</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
Also I use Apache Karaf OSGI container for deploying and that's why my app is a bundle packaging type. 另外,我使用Apache Karaf OSGI容器进行部署,这就是为什么我的应用程序是捆绑包装类型的原因。 There is configuration for that:
有相应的配置:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<instructions>
<Import-Package>
!*,
javax.ws.rs,
javax.ws.rs.core,
javax.ws.rs.ext,
org.apache.cxf.jaxrs.impl.tl,
</Import-Package>
<Embed-Transitive>true</Embed-Transitive>
</instructions>
</configuration>
</plugin>
Without javax.ws.rs.ext
and org.apache.cxf.jaxrs.impl.tl
in import-package section I got one of these exceptions: 在import-package部分中没有
javax.ws.rs.ext
和org.apache.cxf.jaxrs.impl.tl
,我得到以下异常之一:
Caused by: java.lang.IllegalArgumentException: interface javax.ws.rs.ext.Providers is not visible from class loader
Caused by: java.lang.IllegalArgumentException: interface org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy is not visible from class loader
So now I have a follow exception when try to deploy my app: 所以现在当尝试部署我的应用程序时,我有一个关注异常:
Caused by: java.lang.IllegalArgumentException: Can not set javax.servlet.http.HttpServletRequest field MyService.request to org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest
And I spent a lot of time to figure out what's the problem.. But one thing I note: 我花了很多时间来找出问题所在。 但是我要注意的一件事是:
if I use ThreadLocalHttpServletRequest request
instead of HttpServletRequest request
, then it works without exceptions, but the request
field is null
when I try to use it from any methods. 如果我使用
ThreadLocalHttpServletRequest request
而不是HttpServletRequest request
,则它可以正常工作,但是当我尝试通过任何方法使用它时, request
字段为null
。
What should I do to make it works properly with HttpServletReqest
? 我应该怎么做才能使其与
HttpServletReqest
正常工作?
try to use org.apache.cxf.jaxrs.ext.MessageContext 尝试使用org.apache.cxf.jaxrs.ext.MessageContext
import javax.ws.rs.core.Context;
import org.apache.cxf.jaxrs.ext.MessageContext;
// your code goes like this
@Context
private MessageContext context;
// try to get the request/response/session etc in your methods
HttpServletRequest req = context.getHttpServletRequest();
HttpServletResponse res = context.getHttpServletResponse()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.