我仍然有这个问题。这是我的解决方法是编写一个 ServletRequestListener 将请求放入 ThreadLocal var。然后WebService就可以从ThreadLocal获取请求了。换句话说,我正在重新实现一些对我不起作用的东西。
这是监听器:
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class SDMXRequestListener implements ServletRequestListener {
public SDMXRequestListener() {
}
public void requestDestroyed(ServletRequestEvent event) {
}
public void requestInitialized(ServletRequestEvent event) {
final ServletRequest request = event.getServletRequest();
ServletRequestStore.setServletRequest(request);
}
}
这是 ThreadLocal 包装器:
import javax.servlet.ServletRequest;
public class ServletRequestStore {
private final static ThreadLocal<ServletRequest> servletRequests = new ThreadLocal<ServletRequest>();
public static void setServletRequest(ServletRequest request) {
servletRequests.set(request);
}
public static ServletRequest getServletRequest() {
return servletRequests.get();
}
}
和 web.xml 接线:
<listener>
<listener-class>ecb.sdw.webservices.SDMXRequestListener</listener-class>
</listener>
Web 服务使用以下代码获取请求:
最终 HttpServletRequest 请求 = (HttpServletRequest) ServletRequestStore.getServletRequest();