本文介绍了wsContext.getMessageContext().get(MessageContext.SERVLET_REQUEST)在Jetty上返回null吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的扩展名为此问题.我在 Jetty服务器上运行了确切的代码,而其他 SOAP 网络服务也可以正常运行.但是,在这一行:

I have an extension of this question. I have that exact code running on a Jetty Server, and other SOAP web services work perfectly. However, on this line:

HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST); 
System.out.println("Client IP = " + req.getRemoteAddr()); 

服务器因空指针异常而崩溃. mc.get(MessageContext.SERVLET_REQUEST)返回null.

The server crashes with a null pointer exception. mc.get(MessageContext.SERVLET_REQUEST) is returning null.

通过比较,mc.get(MessageContext.HTTP_REQUEST_METHOD)返回"POST",所以我认为这是可行的.

By comparison, mc.get(MessageContext.HTTP_REQUEST_METHOD) returns "POST", so I assume that's working.

该如何解决?

我已经尝试过修复无济于事.

I've tried this fix to no avail.

我也尝试使用@Context注释来替代,并且遇到相同的问题.

I've also tried using the @Context annotation instead and got the same issue.

System.out.println(mc)产生以下结果:

A System.out.println(mc) yields this:

{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具有此值?

and so on, and the list of values does NOT include javax.xml.ws.servlet.request, which is the value of MessageContext.SERVLET_REQUEST. What do I need to do to make sure the MessageContext has this value?

推荐答案

当前,Jetty HTTP SPI JAX-WS实现似乎无法正确地将MessageContext注入到Web服务中.尝试改用Apache CXF.拥有

Currently the Jetty HTTP SPI JAX-WS implementation doesn't appear to properly inject the MessageContext into a web service. Try switching to Apache CXF instead. Once you have

  1. cxf-2.6.2.jar
  2. neethi-3.0.2.jar
  3. xmlschema-core-2.0.3.jar

在项目构建路径上,您必须创建一个Servlet类,该类扩展CXFNonSpringServlet并覆盖loadBus函数,如下所示:

on your project build path, you have to create a servlet class that extends the CXFNonSpringServlet and overrides the loadBus function like so:

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);
    }
}

然后在配置服务器的位置,添加以下行:

And then where you are configuring your server, add these lines:

    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();

这篇关于wsContext.getMessageContext().get(MessageContext.SERVLET_REQUEST)在Jetty上返回null吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 16:45