在宁静的Web服务中访问HttpServletRequest对象 [英] Getting Access to HttpServletRequest object in restful web service

查看:133
本文介绍了在宁静的Web服务中访问HttpServletRequest对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以访问soap Web服务中的HttpServlet Request对象,如下所示:
在服务实现中声明WebServiceContext的私有字段,并将其注释为资源:

I can get access to the HttpServlet Request object in a soap web service as follows: Declaring a private field for the WebServiceContext in the service implementation, and annotate it as a resource:

@Resource
private WebServiceContext context;

要获取HttpServletRequet对象,我编写如下代码:

To get the HttpServletRequet object, I write the code as below:

MessageContext ctx = context.getMessageContext();
HttpServletRequest request =(HttpServletRequest)ctx.get(AbstractHTTPDestination.HTTP_REQUEST);

但这些东西在宁静的网络服务中无效。
我正在使用Apache CXF开发restful web服务。请告诉我如何才能访问HttpServletRequest对象。

But these things are not working in a restful web service. I am using Apache CXF for developing restful web service. Please tell me how can I get access to HttpServletRequest Object.

推荐答案

我建议使用 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)。请参阅上下文注释。

You can use the @Context annotation to flag other types (such as ServletContext or the HttpServletRequest specifically). See Context Annotations.

这篇关于在宁静的Web服务中访问HttpServletRequest对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆