我可以访问 soap web 服务中的 HttpServlet Request 对象,如下所示: 在服务实现中为 WebServiceContext 声明一个私有(private)字段,并将其注释为资源:
@Resource
private WebServiceContext context;
为了获取 HttpServletRequet 对象,我编写了如下代码:
MessageContext ctx = context.getMessageContext();
HttpServletRequest request =(HttpServletRequest)ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
但是这些东西在 restful web 服务中是行不通的。 我正在使用 Apache CXF 开发 Restful 网络服务。请告诉我如何才能访问 HttpServletRequest 对象。
最佳答案
我建议使用 org.apache.cxf.jaxrs.ext.MessageContext
import javax.ws.rs.core.Context;
import org.apache.cxf.jaxrs.ext.MessageContext;
...
// add the attribute to your implementation
@Context
private MessageContext context;
...
// then you can access the request/response/session etc in your methods
HttpServletRequest req = context.getHttpServletRequest();
HttpServletResponse res = context.getHttpServletResponse()
您可以使用 @Context
注释来标记其他类型(例如 ServletContext 或具体的 HttpServletRequest)。参见 Context Annotations.
关于java - 在 restful web 服务中访问 HttpServletRequest 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7873197/