上篇 WebService小白学习 之 处理一些Map等复杂类型
本篇为CXF添加拦截器,自定义拦截器。
实现过程:
1、在服务端项目Server.java添加日记拦截器
package com.gx.server;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.gx.webservice.IHelloWorld;
import com.gx.webservice.impl.HelloWorldImpl;
public class Server {
static String address = "http://127.0.0.1:34563/helloWorld";
@SuppressWarnings("deprecation")
public static void main(String[] args) {
System.out.println("web service start");
IHelloWorld implementor = new HelloWorldImpl();
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.setAddress(address); //设置暴露地址
factoryBean.setServiceClass(IHelloWorld.class); //设置暴露接口类
factoryBean.setServiceBean(implementor); //设置实现类
//拦截器
factoryBean.getInInterceptors().add(new LoggingInInterceptor());//in,日记拦截器
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); //out,日记拦截器
factoryBean.create(); //创建webservice接口
System.out.println("web service started");
}
}
运行客户端Client.java查看效果
2、在客户端Client.java添加日记拦截器,需要在pom.xml先添加包,和服务端一样的,忘记看这里
package com.gx.webservice;
import java.util.List;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
public class Client {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
IHelloWorldService service = new IHelloWorldService();
IHelloWorld helloworld = service.getIHelloWorldPort();
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(helloworld);
client.getInInterceptors().add(new LoggingInInterceptor()); //添加in日记拦截器
client.getOutInterceptors().add(new LoggingOutInterceptor()); //添加out日记拦截器
//System.out.println(helloworld.say("zj"));
/*User user = new User();
user.setUsername("sa");
user.setPassword("123");
List<Role> rolelist = helloworld.getRoleByUser(user);
for (Role role : rolelist) {
System.out.println(role.getId()+","+role.getRoleName());
}*/
MyRoleArray array = helloworld.getRoles();
List<MyRole> roleList = array.item;
for (MyRole myRole : roleList) {
System.out.print(myRole.key+":");
for (Role role : myRole.value) {
System.out.print(role.getId()+","+role.getRoleName());
}
System.out.println("=================");
}
}
}
运行客户端Client.java查看效果,Intbound Message 、Outbound Message 和服务端相反
3、服务端添加自定义拦截器,创建com.gx.interceptor包,创建MyInInterceptor.java
package com.gx.interceptor;
import java.util.List;
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;
import org.w3c.dom.NodeList;
public class MyInInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
public MyInInterceptor() {
super(Phase.PRE_INVOKE); //在调用方法前调用自定义拦截器
}
@SuppressWarnings("null")
@Override
public void handleMessage(SoapMessage message) throws Fault {
//自定义拦截器内容
List<Header> headers = message.getHeaders();
if(headers==null && headers.size()==0){
throw new Fault(new IllegalArgumentException("没有Header,拦截器拦截"));
}
Header firstHeader = headers.get(0);
Element ele = (Element) firstHeader.getObject();
NodeList uList = ele.getElementsByTagName("userName");
NodeList pList = ele.getElementsByTagName("passworld");
if(uList.getLength()!=1){
throw new Fault(new IllegalArgumentException("用户名格式不对"));
}
if(pList.getLength()!=1){
throw new Fault(new IllegalArgumentException("密码格式不对"));
}
String userName = uList.item(0).getTextContent();
String password = pList.item(0).getTextContent();
if(!userName.equals("sa") || !password.equals("123")){
throw new Fault(new IllegalArgumentException("用户名或密码错误"));
}
}
}
在Server.java添加 factoryBean.getInInterceptors().add(new MyInInterceptor());//自定义拦截器
4、客户端添加自定义拦截器,创建com.gx.interceptor包,创建AddHeaderInterceptor.java
package com.gx.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.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class AddHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
private String userName;
private String password;
public AddHeaderInterceptor(String userName, String password) {
super(Phase.PREPARE_SEND); //准备发射soap消息之前
this.userName = userName;
this.password = password;
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
//自定义拦截器处理内容,和服务器拦截器内容对应,这里为创建头Header
List<Header> headerList = message.getHeaders();
Document doc = DOMUtils.createDocument();
Element ele = doc.createElement("authHeader");
Element uElement = doc.createElement("userName");
uElement.setTextContent(userName);
Element pElement = doc.createElement("passworld");
pElement.setTextContent(password);
ele.appendChild(uElement);
ele.appendChild(pElement);
headerList.add(new Header(new QName("sa"), ele));
}
}
在Client.java添加 client.getOutInterceptors().add(new AddHeaderInterceptor("sa","123")); //添加自定义拦截器
5、服务端发布服务,客户端测试
ok,看到发送内容,把client.getOutInterceptors().add(new AddHeaderInterceptor("sa","123"));的"123"改1234“错误密码测试
可看到自定义拦截器已拦截并throw错误了。
下篇
WebService小白学习 之 Spring整合CXF,添加拦截器