(五)CXF之添加拦截器

本文介绍如何在WebService中使用拦截器,包括服务端和客户端的配置方法,并通过具体案例展示了拦截器的日志记录功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、需求分析

  • webService中的拦截器类似于servlet的Filter过滤器。一般用于调用服务前后先调用拦截器的方法。

 

二、案例

  • 本章案例是基于上一章节的基础上添加拦截器的

  2.1  服务端添加拦截器

ublic class PublishMain {
    public static void main(String[] args) {
        
        String address="http://localhost:3333/login";
        JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
        factoryBean.setAddress(address);
        factoryBean.setServiceClass(ILogin.class);
        factoryBean.setServiceBean(new Login());
        /**
         * 添加In拦截器 日志拦截器
         * 添加Out拦截器 日志拦截器
         */
        factoryBean.getInInterceptors().add(new LoggingInInterceptor());
        factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
        factoryBean.create();
        System.out.println("服务发布.......");
        
    }
}

  2.2  在客户端添加拦截器:必须引入cxf的jar包

public class Client {
    public static void main(String[] args) {
        ILogin login=new ILoginService().getILoginPort();
        
        org.apache.cxf.endpoint.Client client=ClientProxy.getClient(login);
        
   /**
* 添加In拦截器 日志拦截器
* 添加Out拦截器 日志拦截器
*/


client.getInInterceptors().add(new LoggingInInterceptor()); client.getOutInterceptors().add(new LoggingOutInterceptor()); MyRoleArray roles=login.getRoles(); List<MyRole> roleList= roles.item; for(MyRole role:roleList) { System.out.println(role.getKey()); for(Role r:role.getValue()) { System.out.println(r.getId()+"\t"+r.getRoleName()); } System.out.println("------------------"); } } }

 

  2.3  测试

  • 上面的配置已经把拦截器添加好了,当发布服务后,客户端执行后,首先,查看客户端的控制台

 由此可见,客户端是先进后出(先发送请求再接收数据)。

  • 再查看服务端的控制台,可见服务端是先进后出。

 

转载于:https://www.cnblogs.com/shyroke/p/7966148.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值