2016-03-22 11 views
0

我通过实现这样的接口写入(Java 6中)一个简单的Web服务:JWS检索当Web服务被称为IP地址

import javax.jws.WebMethod; 
import javax.jws.WebService; 
import javax.jws.soap.SOAPBinding; 
import javax.jws.soap.SOAPBinding.Style; 

@WebService 
@SOAPBinding(style = Style.RPC) 
public interface MyWebService { 
    @WebMethod 
    String helloWorld(); 
} 

所以,我的具体类是一样的东西:

import javax.jws.WebService; 

@WebService(endpointInterface="xxx", 
      portName="yyy", 
      serviceName="zzz") 
public class MyConcreteWS implements MyWebService { 
    @Override 
    public String helloWorld() { 
     // ... 
    } 
} 

现在,在方法的helloWorld()我怎么能检索有关来电者的任何信息,例如像IP地址?

我没有开发一个servlet,它只是一个简单的罐子,里面有一个主要的方法。

回答

0

不知道这是否适用于您的情况,但您可以尝试注入资源WebServiceContext并使用它获取IP。

public class MyConcreteWS implements MyWebService { 
    @Resource WebServiceContext wsContext; 

    @Override 
    public String helloWorld() { 
     MessageContext mc = wsContext.getMessageContext(); 
     HttpServletRequest req = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST); 
     String ip = req.getRemoteAddr(); 
     // ... 
    } 
} 
+0

不幸的是,它不起作用。我在调用对象* req *上的方法* getRemoteAddr()*时检索'NullPointerException'。 –