我的扩展名为this question。我在Jetty服务器上运行了确切的代码,并且其他SOAP Web服务也可以正常工作。但是,在这一行:
HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST);
System.out.println("Client IP = " + req.getRemoteAddr());
服务器因空指针异常而崩溃。
mc.get(MessageContext.SERVLET_REQUEST)
返回null。相比之下,
mc.get(MessageContext.HTTP_REQUEST_METHOD
)返回“ POST”,因此我认为这是可行的。我该怎么做才能解决此问题?
编辑:
我试过this修复无济于事。
我也尝试使用@Context批注来代替,并且遇到了同样的问题。
System.out.println(mc)产生以下结果:
{javax.xml.ws.wsdl.port={http://my.test.namespace.com/}testWSDLPort,
javax.xml.ws.soap.http.soapaction.uri="",
com.sun.xml.internal.ws.server.OneWayOperation=null,
javax.xml.ws.http.request.pathinfo=null,
...
...
等等,并且值列表不包含javax.xml.ws.servlet.request,它是MessageContext.SERVLET_REQUEST的值。我需要做什么以确保MessageContext具有此值?
最佳答案
当前,Jetty HTTP SPI JAX-WS实现似乎无法正确地将MessageContext注入到Web服务中。尝试改用Apache CXF。一旦有了
cxf-2.6.2.jar
neethi-3.0.2.jar
xmlschema-core-2.0.3.jar
在项目构建路径上,您必须创建一个Servlet类,该类扩展CXFNonSpringServlet并覆盖loadBus函数,如下所示:
public class SOAPServlet extends CXFNonSpringServlet {
private static final long serialVersionUID = 1L;
private Map<String, Object> endpoints;
public SOAPServlet(){
endpoints = new HashMap<String, Object>();
}
@Override
public void loadBus(ServletConfig servletConfig) {
super.loadBus(servletConfig);
// You could add the endpoint publish codes here
Bus bus = getBus();
BusFactory.setDefaultBus(bus);
Set s = endpoints.entrySet();
Iterator p = s.iterator();
while(p.hasNext()){
Map.Entry m = (Map.Entry)p.next();
String address = (String)m.getKey();
Object impl = (Object)m.getValue();
System.out.println("Publishing " + address);
Endpoint.publish(address, impl);
}
}
public void publish(String address, Object impl){
endpoints.put(address, impl);
}
}
然后在配置服务器的位置添加以下行:
Server server = new Server(8080);
// Configure SOAP servlet
SOAPServlet servlet = new SOAPServlet();
ServletHolder SOAPServletHolder = new ServletHolder(servlet);
ServletContextHandler SOAPContext = new ServletContextHandler(server,"/",ServletContextHandler.SESSIONS);
SOAPContext.addServlet(SOAPServletHolder, "/*");
// Set server context handlers
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler []{SOAPContext});
server.setHandler(contexts);
// Publish SOAP Web service endpoints
servlet.publish("/MyWebServiceRelativeURL", new MyWebServiceImpl());
server.start();
server.join();