完整版见https://jadyer.github.io/2013/05/31/jaxws-work-with-spring-and-get-servletapi/
服务端是Web Project,客户端是Java Project,先看服务端代码
首先是SEI,即服务端接口类HelloService.java
package com.jadyer.service;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(targetNamespace="http://blog.csdn.net/jadyer")
public interface HelloService {
@WebResult(name="sayHelloResult")
public String sayHello(@WebParam(name="name")String name);
}
package com.jadyer.service;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import org.springframework.stereotype.Service;
import com.jadyer.dao.HelloDaoJDBC;
/**
* JAX-WS与Spring集成时获取HttpServletAPI
* 1)SIB中直接使用@Resource注入javax.xml.ws.WebServiceContext
* 2)使用wsc.getMessageContext().get(SOAPMessageContext.SERVLET_REQUEST)获取HttpServletRequest
* 但前提是SIB已被Spring所管理..本例中的明显特征是SIB上使用了@Service注解
* @create May 27, 2013 6:10:50 PM
* @author 玄玉
*/
@WebService(serviceName="myHelloService",
targetNamespace="http://blog.csdn.net/jadyer",
endpointInterface="com.jadyer.service.HelloService")
@Service
public class HelloServiceImpl implements HelloService {
@Resource
private HelloDaoJDBC helloDao;
@Resource
private WebServiceContext wsc;
@Override
public String sayHello(String name) {
MessageContext ctx = wsc.getMessageContext();
HttpServletRequest request = (HttpServletRequest)ctx.get(SOAPMessageContext.SERVLET_REQUEST);
System.out.println("----------------------------------------------------------------------");
System.out.println("ServletContextName=" + request.getSession().getServletContext().getServletContextName());
System.out.println("ContextPath=" + request.getSession().getServletContext().getContextPath());
System.out.println("RealPath=" + request.getSession().getServletContext().getRealPath("/"));
System.out.println("ServerInfo=" + request.getSession().getServletContext().getServerInfo());
System.out.println("----------------------------------------------------------------------");
return helloDao.sayHello(name);
}
}
package com.jadyer.dao;
import org.springframework.stereotype.Repository;
@Repository("helloDao")
public class HelloDaoJDBC {
public String sayHello(String name) {
System.out.println("Receive the name=[" + name + "]");
if(null==name){
return "Hello,World";
}else{
return "Hello," + name;
}
}
}
package com.jadyer.handler;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class LicenseHandler implements SOAPHandler {
@Override
public Set getHeaders() {
return null;
}
@Override
public void close(MessageContext context) {
}
@Override
public boolean handleFault(SOAPMessageContext context) {
System.out.println("Server.handleFault() is invoked......");
return false;
}
@Override
@SuppressWarnings("unchecked")
public boolean handleMessage(SOAPMessageContext context) {
System.out.println("Server.handleMessage() is invoked......");
//只有Handler被纳入Spring管理后,这里获取到的HttpServletRequest才不是null
HttpServletRequest request = (HttpServletRequest)context.get(SOAPMessageContext.SERVLET_REQUEST);
System.out.println("RealPath=" + request.getSession().getServletContext().getRealPath("/"));
//从服务端角度看:inbound表示接收客户端消息,outbound表示响应消息给客户端..从客户端角度看时正好与之相反
Boolean isOutBound = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if(isOutBound){
return true;
}
SOAPMessage message = context.getMessage();
SOAPHeader header = null;
SOAPBody body = null;
try {
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
header = envelope.getHeader();
body = envelope.getBody();
} catch (SOAPException e) {
e.printStackTrace();
}
//获取Body中的part name
String partName = body.getChildNodes().item(0).getLocalName();
//只对服务端开放的login()方法进行验证,否则它会对服务端开放的所有方法进行验证
if(!"sayHello".equals(partName)){
return true;
}
if(null==header){
System.out.println("未找到头信息......");
return true;
}
Iterator iterator = header.extractAllHeaderElements();
if(!iterator.hasNext()){
System.out.println("头信息不能为空......");
return true;
}
System.out.println("协议有效......");
while(iterator.hasNext()){
System.out.println(iterator.next().getTextContent());
}
return true;
}
}
#http://blog.csdn.net/jadyer/article/details/7833395
log4j.rootLogger=DEBUG,CONSOLE
#通常用于框架日志,如mina,spring等
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%d{yyyyMMdd HH:mm:ss}][%t][%C{1}]%m%n
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
jaxws-servlet
com.sun.xml.ws.transport.http.servlet.WSSpringServlet
jaxws-servlet
/myService
首先是客户端自定义的HeaderHandler.java
package com.jadyer.handler;
import java.io.IOException;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class HeaderHandler implements SOAPHandler {
@Override
public Set getHeaders() {
return null;
}
@Override
public void close(MessageContext context) {
}
@Override
public boolean handleFault(SOAPMessageContext context) {
System.out.println("\nClient.handleFault() is invoked.....");
return false;
}
@Override
public boolean handleMessage(SOAPMessageContext context) {
System.out.println("\nClient.handleMessage() is invoked.....");
Boolean isOutBound = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if(isOutBound){
SOAPMessage message = context.getMessage();
try {
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();
String partName = envelope.getBody().getFirstChild().getLocalName();
if("sayHello".equals(partName)){
if(null == header){
header = envelope.addHeader();
}
QName qname = new QName("http://blog.csdn.net/jadyer", "licenseInfo", "ns");
header.addHeaderElement(qname).setValue("Jadyer");
message.writeTo(System.out);
}
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}
package com.jadyer.handler;
import java.util.ArrayList;
import java.util.List;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;
import org.springframework.stereotype.Component;
@Component
public class HeaderHandlerResolver implements HandlerResolver {
@SuppressWarnings("unchecked")
@Override
public List getHandlerChain(PortInfo portInfo) {
List handlers = new ArrayList();
handlers.add(new HeaderHandler());
return handlers;
}
}
package com.jadyer.service;
import javax.annotation.Resource;
import net.csdn.blog.jadyer.HelloService;
import org.springframework.stereotype.Service;
@Service
public class ClientService {
@Resource
private HelloService myServerWebService;
public String getServerResponse(String name){
return myServerWebService.sayHello(name);
}
}
注意:具体的客户端WebService代码由wsimport生成,详见http://blog.csdn.net/jadyer/article/details/8692108
package com.jadyer.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jadyer.service.ClientService;
public class ClientApp {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ClientService cs = (ClientService)ctx.getBean("clientService");
System.out.println(cs.getServerResponse("玄玉"));
}
}
//客户端
Client.handleMessage() is invoked.....
Jadyer 玄玉
Client.handleMessage() is invoked.....
Hello,玄玉
//服务端
Server.handleMessage() is invoked......
RealPath=D:\Develop\apache-tomcat-6.0.36\webapps\jaxws-spring\
协议有效......
Jadyer
----------------------------------------------------------------------
ServletContextName=null
ContextPath=/jaxws-spring
RealPath=D:\Develop\apache-tomcat-6.0.36\webapps\jaxws-spring\
ServerInfo=Apache Tomcat/6.0.36
----------------------------------------------------------------------
Receive the name=[玄玉]
Server.handleMessage() is invoked......
RealPath=D:\Develop\apache-tomcat-6.0.36\webapps\jaxws-spring\