zoukankan      html  css  js  c++  java
  • 学习webservice之cxf(6):cxf内置拦截器

    在Client服务中的pom.xml文件中添加依赖

        <dependencies>
              <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxws</artifactId>
                <version>3.2.5</version>
            </dependency>
              <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-core</artifactId>
                <version>3.2.5</version>
            </dependency>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-transports-http-jetty</artifactId>
                <version>3.2.5</version>
            </dependency>
          </dependencies>

    在Server服务中添加日志拦截器

    package com.rg2.webservice.impl;
    
    import javax.xml.ws.Endpoint;
    
    import org.apache.cxf.interceptor.LoggingInInterceptor;
    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
    
    import com.rg2.webservice.HelloWorld;
    
    public class Server {
    
        public static void main(String[] args) {
            System.out.println("web service start");
            HelloWorld implementor = new HelloWorldImpl();
            String address = "http://localhost/helloWorld";
    //        Endpoint.publish(address, implementor);//jdk实现暴露webservice接口
            JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
            factoryBean.setAddress(address);//设置暴露地址
            factoryBean.setServiceClass(HelloWorld.class);//接口类
            factoryBean.setServiceBean(implementor);//设置实现类
            factoryBean.getInInterceptors().add(new LoggingInInterceptor());//添加in拦截器 日志拦截器
            factoryBean.getOutInterceptors().add(new LoggingInInterceptor());//添加out拦截器 日志拦截器
            factoryBean.create();//创建webservice接口
            System.out.println("web service started");
        }
    
    }

    在Client服务中添加日志拦截器

    package com.rg2.webservice;
    
    import java.util.List;
    
    import org.apache.cxf.frontend.ClientProxy;
    import org.apache.cxf.interceptor.LoggingInInterceptor;
    
    public class Client {
    
        public static void main(String[] args) {
            HelloWorldService service = new HelloWorldService();
            HelloWorld helloWorldPort = service.getHelloWorldPort();
            org.apache.cxf.endpoint.Client client = ClientProxy.getClient(helloWorldPort);
            client.getInInterceptors().add(new LoggingInInterceptor());//添加in拦截器 日志拦截器
            client.getOutInterceptors().add(new LoggingInInterceptor());//添加out拦截器 日志拦截器
            
            MyRoleArray getroles = helloWorldPort.getroles();
            List<MyRole> roleList = getroles.item;
            for (int i = 0; i < roleList.size(); i++) {
                MyRole my = roleList.get(i);
                System.out.print(my.key + ":");
                for (Role role : my.value) {
                    System.out.print(role.getId()+","+role.getRoleName());
                }
                System.out.println("===============");
            }
        }
    
    }
  • 相关阅读:
    我所认识的JavaScript
    谈如何边做事,边提高
    对JavaScript开发中困扰的思考
    Soundex语音算法
    Perl分割字符串的一个精妙的写法
    Perl深拷贝
    Git diff hash顺序的问题
    perl命令行批量修改文件内容
    IOS写文件
    删除Linux乱码文件
  • 原文地址:https://www.cnblogs.com/zhengyuanyuan/p/9277235.html
Copyright © 2011-2022 走看看