WebService学习总结十 使用Spring发布WebService并添加拦截器

首先使用Spring方式发布成功WebService,再在客户端和服务器端引入出拦截器和入拦截器,引入的方式是写在配置文件中的。


客户端:

自定义的拦截器

package ws.client.interceptor;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.xml.utils.DOMHelper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class MyClientInterceptor extends AbstractPhaseInterceptor {
	//自定义的校验字段,姓名和密码
	private String name;
	private String pwd;
	
	public MyClientInterceptor(String name,String pwd) {
		super(Phase.PRE_PROTOCOL);//配置拦截时机,一定要有,准备协议化得时候拦截
		this.name=name;
		this.pwd=pwd;
	}

	//封装数据到消息里,格式如下
	/*    
	 * 			 //里面的标签可以有多个
	 * 				  //自定义标签,随意
	 * 					zhangsan
	 * 					123123
	 * 				
	 * 				  //自定义标签,随意
	 * 					zhangsan
	 * 					123123
	 * 				
	 * 			
	 * 			
	 * 				
	 * 					BOB
	 * 				
	 * 			
	 * 	  
	 * 
	 * 
	 * 
	 * 
	 *
	 *
	 *
	 *
	 */
	@Override
	public void handleMessage(SoapMessage msg) throws Fault {
		List
headers=msg.getHeaders(); //创建Head //创建head里的元素,添加到head Document document=DOMHelper.createDocument(); Element atguigu=document.createElement("atguigu"); Element name=document.createElement("name"); name.setTextContent(this.name); atguigu.appendChild(name); Element pwd=document.createElement("pwd"); pwd.setTextContent(this.pwd); atguigu.appendChild(pwd); headers.add(new Header(new QName("atguigu"), atguigu));//QName里的参数要和标签名一样 System.out.println("client:handleMessage"); } }

wsimport生成的客户端代码:

GetUserById.java
GetUserByIdResponse.java
ObjectFactory.java
package-info.java
UserBean.java
UserWS.java
UserWSImplService.java


客户端的配置:



		
		
		
			
			
				
				
			
		
	


客户端测试类:

package ws.spring.server;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestClient {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(
				new String[]{"client-beans.xml"});
		UserWS userWS=(UserWS) context.getBean("userClient");
		UserBean userBean=userWS.getUserById(1);
		System.out.println("Client:"+userBean);
	}

}



服务器端:

自定义的拦截器:

package ws.server.interceptor;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
/**
 * 检查用户名称和密码
 * @author
 *
 */
public class MyServerInterceptor extends AbstractPhaseInterceptor {

	public MyServerInterceptor() {
		super(Phase.PRE_PROTOCOL);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void handleMessage(SoapMessage msg) throws Fault {
		// 读header里的数据
		Header header=msg.getHeader(new QName("atguigu"));//参数和客户端传的一样
		if(header!=null){
			Element atguigu=(Element) header.getObject();
			String name=atguigu.getElementsByTagName("name").item(0).getTextContent();
			String pwd=atguigu.getElementsByTagName("pwd").item(0).getTextContent();
			if(name.equals("zhangsan")&&pwd.equals("123")){
				System.out.println("server:check ok");
				return;
			}
		}
		//没通过校验,抛出异常
		System.out.println("没有通过校验");
		throw new Fault(new RuntimeException("用户名或密码不对"));
	}

}


bean:

package ws.spring.server;

public class UserBean {
	private int id;
	private String name;
	
	public UserBean(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "UserBean [id=" + id + ", name=" + name + "]";
	}
	
}


SEI:

package ws.spring.server;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface UserWS {
	@WebMethod
	public UserBean getUserById(int id);
}


SEIImpl

package ws.spring.server;

import javax.jws.WebService;

@WebService
public class UserWSImpl implements UserWS {
	public UserWSImpl(){
		System.out.println("初始化 UserWSImpl");
	}
	@Override
	public UserBean getUserById(int id) {
		System.out.println("server getUserById:"+id);
		return new UserBean(1, "张三");
	
	}

}


配置文件



  
  
  
    
   
    
   
   
     	
     		
     		
     	
    
     


web.xml



  ws_spring_interceptor_server
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
    contextConfigLocation
    classpath:beans.xml
  
  
    
         org.springframework.web.context.ContextLoaderListener
      
  
  
    CXFServlet
    
         org.apache.cxf.transport.servlet.CXFServlet
      
    1
  
  
    CXFServlet
    /*
  




你可能感兴趣的:(WebService)