1.自定义拦截器
public class MyInterceptor extends AbstractPhaseInterceptor<Message> {
public MyInterceptor(String phase) {
super(phase);
}
public MyInterceptor() {
super(Phase.RECEIVE);
}
public void handleFault(Message arg0) {
System.out.println("Error");
}
public void handleMessage(Message arg0) throws Fault {
System.out.println("Hello, This is my Interceptor.");
}
}
2.在服务器端或者客户端加上
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new MyInterceptor());
factory.getOutInterceptors().add(new
MyInterceptor(Phase.SEND));
这里有几点需要注意:
a. 我们的拦截器是不针对服务端还是客户端的。就是说一个实现可以用在两个地方。
b. 继承AbstractPhaseInterceptor 至少要实现一个带参数的构造方法,我们实现了一个
带String 类型参数的构造方法和一个默认无参构造方法。在无参构造方法中,有一句:
super(Phase.RECEIVE); 其 中 Phase.RECEIVE 表 示 这 个 实 例 处 理 的
InInterceptors 过程,即便改实例被设置为OutInterceptors,也不会起任何作用。
同理Phase.SEND 是处理OutInterceptors 过程的, 即便改实例被设置为
InInterceptors,也不会起任何作用。