萨吕,
我是Weblogic WebService实现的新手。尝试使用JAX-RPC从SOAP消息中获取客户端的远程地址。需要从SOAP消息头获取客户端的远程IP。根据BEA / Oracle文档尝试了许多解决方案。 WebService java文件:
@WebService
public class clientsIPWs {
@Control
private SoapMessageControl smc;
@WebMethod
@WebResult(name="return")
public String getClientAddress() {
return smc.getRemoteAddress();
}
}
SoapMessageControl.java
@ControlInterface
public interface SoapMessageControl {
public String getRemoteAddress();
}
SoapMessageControlImpl.java
@ControlImplementation
public class SoapMessageControlImpl implements SoapMessageControl, Serializable {
private static final long serialVersionUID = 1L;
@Context
private SOAPMessageContext smc;
@WebMethod()
public String getRemoteAddress() {
HttpServletRequest request =
(HttpServletRequest) smc.getProperty("transport.http.servletRequest");
return request.getRemoteAddr();
}
}
在最后一个尝试使用@Context的许多变体,如MessageContext / WebServiceContext(这个不推荐使用),但是每次测试都会收到这样的异常:
无法调用结束组件无法调用方法 显示java.lang.NullPointerException 在controls.SoapMessageControlImpl.getRemoteAddress(SoapMessageControlImpl.java:33) 在controls.SoapMessageControlBean.getRemoteAddress(SoapMessageControlBean.java:119) at services.CustProdWs.getClientAddress(CustProdWs.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在java.lang.reflect.Method.invoke(Method.java:585) 在weblogic.wsee.component.pojo.JavaClassComponent.invoke(JavaClassComponent.java:99) 在weblogic.wsee.ws.dispatch.server.ComponentHandler.handleRequest(ComponentHandler.java:64) 在weblogic.wsee.handler.HandlerIterator.handleRequest(HandlerIterator.java:127) 在weblogic.wsee.ws.dispatch.server.ServerDispatcher.dispatch(ServerDispatcher.java:85) 在weblogic.wsee.ws.WsSkel.invoke(WsSkel.java:80) 在weblogic.wsee.server.servlet.SoapProcessor.handlePost(SoapProcessor.java:66) 在weblogic.wsee.server.servlet.SoapProcessor.process(SoapProcessor.java:44) 在weblogic.wsee.server.servlet.BaseWSServlet $ AuthorizedInvoke.run(BaseWSServlet.java:181) 在weblogic.wsee.server.servlet.BaseWSServlet.service(BaseWSServlet.java:92) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:856) 在weblogic.servlet.internal.StubSecurityHelper $ ServletServiceAction.run(StubSecurityHelper.java:227) 在weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125) 在weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283) 在weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175) at weblogic.servlet.internal.WebAppServletContext $ ServletInvocationAction.run(WebAppServletContext.java:3244) 在weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) 在weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121) 在weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2010) 在weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1916) 在weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1366) 在weblogic.work.ExecuteThread.execute(ExecuteThread.java:209) 在weblogic.work.ExecuteThread.run(ExecuteThread.java:181)
在谷歌中,几乎每个解决方案都使用了JAX-WS,这在我的框架中是不可用的。 感谢。
答案 0 :(得分:0)
这个解决方案对我有用:
获取HttpServletRequest / Response对象
如果您的Web服务使用HTTP作为其传输协议,则可以使用 weblogic.wsee.connection.transport.servlet.HttpTransportUtils API 获取javax.servlet.http.HttpServletRequest和 来自JAX-RPC的javax.servlet.http.HttpServletResponse对象 ServletEndpointContext对象,如以下示例所示 (相关代码以粗体显示并在示例后解释):
package examples.webservices.http_transport_utils;
import javax.xml.rpc.server.ServiceLifecycle;
import javax.xml.rpc.server.ServletEndpointContext;
import javax.xml.rpc.ServiceException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.jws.WebMethod;
import javax.jws.WebService;
import weblogic.jws.WLHttpTransport;
import weblogic.wsee.connection.transport.servlet.HttpTransportUtils;
@WebService(name="HttpTransportUtilsPortType",
serviceName="HttpTransportUtilsService",
targetNamespace="http://example.org")
@WLHttpTransport(contextPath="servlet", serviceUri="HttpTransportUtils",
portName="HttpTransportUtilsPort")
public class HttpTransportUtilsImpl implements ServiceLifecycle {
private ServletEndpointContext wsctx = null;
public void init(Object context) throws ServiceException {
System.out.println("ServletEndpointContext inited...");
wsctx = (ServletEndpointContext)context;
}
public void destroy() {
System.out.println("ServletEndpointContext destroyed...");
wsctx = null;
}
@WebMethod()
public String getServletRequestAndResponse() {
HttpServletRequest request =
HttpTransportUtils.getHttpServletRequest(wsctx.getMessageContext());
HttpServletResponse response =
HttpTransportUtils.getHttpServletResponse(wsctx.getMessageContext());
System.out.println("HttpTransportUtils API used successfully.");
return "HttpTransportUtils API used successfully";
}
}