axis2和cxf取得内置对象
开发webservice的时候使用内置对象的确很头疼,网上找了好多资料才勉强搜集了一下,给大家分享一下吧。
1.axis2获取内置对象
package org.lxh; public class Snippet { public HttpServletRequest getRequest() { MessageContext mc = MessageContext.getCurrentMessageContext(); if (mc == null) { System.out.println("无法获取到MessageContext"); return null; } HttpServletRequest request = (HttpServletRequest)mc.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST); return request; } public HttpServletResponse getResponse() { MessageContext mc = MessageContext.getCurrentMessageContext(); if (mc == null) { System.out.println("无法获取到MessageContext"); return null; } HttpServletResponse response = (HttpServletResponse)mc.getProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE); return response; } }
package org.lxh; import java.util.Date; import javax.annotation.Resource; import javax.jws.WebService; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.xml.ws.WebServiceContext; import javax.xml.ws.handler.MessageContext; import org.apache.cxf.message.Message; import org.apache.cxf.transport.http.AbstractHTTPDestination; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @WebService public class SendSMSImpl implements SendSMS { private Logger log = LoggerFactory.getLogger(this.getClass()); @Resource private WebServiceContext context; public String getIp() { try { MessageContext ctx = context.getMessageContext(); HttpServletRequest request = (HttpServletRequest) ctx .get(AbstractHTTPDestination.HTTP_REQUEST); String ip = request.getRemoteAddr(); return ip; } catch (Exception e) { return "获取ip失败"; } } }