zoukankan      html  css  js  c++  java
  • CXF 自定义拦截器

    此例子来自apache cxf sample。

    /**
     * Licensed to the Apache Software Foundation (ASF) under one
     * or more contributor license agreements. See the NOTICE file
     * distributed with this work for additional information
     * regarding copyright ownership. The ASF licenses this file
     * to you under the Apache License, Version 2.0 (the
     * "License"); you may not use this file except in compliance
     * with the License. You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing,
     * software distributed under the License is distributed on an
     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     * KIND, either express or implied. See the License for the
     * specific language governing permissions and limitations
     * under the License.
     */

    1,定义接口:

    package demo.hw.server;
    
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    @WebService
    public interface HelloWorld {
        String sayHi(@WebParam(name = "text") String text);
    }
    

    2,定义实现:

    package demo.hw.server;
    
    public class HelloWorldImpl implements HelloWorld {
    
        public String sayHi(String text) {
            return "Hello " + text;
        }
    }
    


    3,定义服务启动类:

    package demo.hw.server;
    
    import org.apache.cxf.interceptor.LoggingInInterceptor;
    import org.apache.cxf.interceptor.LoggingOutInterceptor;
    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
    
    import demo.hw.inteceptor.MyInteceptor;
    
    public class Server {
    
        protected Server() throws Exception {
            System.out.println("Starting Server");
            HelloWorldImpl implementor = new HelloWorldImpl();
            JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
            svrFactory.setServiceClass(HelloWorld.class);
            svrFactory.setAddress("http://localhost:9000/helloWorld");
            svrFactory.setServiceBean(implementor);
            svrFactory.getInInterceptors().add(new LoggingInInterceptor());
            svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
            svrFactory.getInInterceptors().add(new MyInteceptor("2"));
            svrFactory.create();
        }
    
        public static void main(String args[]) throws Exception {
            new Server();
            System.out.println("Server ready...");
    
            Thread.sleep(5 * 60 * 1000);
            System.out.println("Server exiting");
            System.exit(0);
        }
    }
    


     

    4,定义客户端:

    package demo.hw.client;
    
    import org.apache.cxf.interceptor.LoggingInInterceptor;
    import org.apache.cxf.interceptor.LoggingOutInterceptor;
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    import demo.hw.server.HelloWorld;
    
    public final class Client {
    
        private Client() {
        } 
    
        public static void main(String args[]) throws Exception {
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            factory.getInInterceptors().add(new LoggingInInterceptor());
            factory.getOutInterceptors().add(new LoggingOutInterceptor());
            factory.setAddress("http://localhost:9000/helloWorld");
            HelloWorld client = factory.create(HelloWorld.class);
            System.out.println(client.sayHi("World"));
        }
    }

    5,定义拦截器:

    package demo.hw.inteceptor;
    
    import java.util.HashMap;
    import java.util.Map;
    import org.apache.cxf.interceptor.Fault;
    import org.apache.cxf.message.Message;
    import org.apache.cxf.phase.AbstractPhaseInterceptor;
    import org.apache.cxf.phase.Phase;
    
    public class MyInteceptor extends AbstractPhaseInterceptor<Message>{
        
        private Map<String,String> paramMap = new HashMap<String,String>();
        private String sign ; 
        
        public MyInteceptor(String sign){
            super(Phase.RECEIVE);
            paramMap.put("1", "robert");
            paramMap.put("2", "green");
            paramMap.put("3", "john");
            paramMap.put("4", "smith");
            this.sign = sign;
        } 
    
        @Override
        public void handleMessage(Message message) throws Fault {
            System.out.println("MyInteceptor: do match person ");
            System.out.println(paramMap.get(sign));
        }
    }
    

    此例子来自apache cxf sample。




     

  • 相关阅读:
    IIS部署.net core 的程序后,如何查看控制台的日志?
    Java中string的编码的详细说明
    explicit禁止被用来执行隐式类型转换。仍可以进行显示转换
    判断一个IP字符串为有效的IP方法
    链接原理
    tcp套接字地址
    c/c++中的__attribute__((weak))使用
    关于multiple definition of 错误说明很详细的文章【转载】
    c/c++中结构体中的位域在大小端设备上的内存存储方式----------位域
    TCP之数据缓冲区大小及其限制
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986848.html
Copyright © 2011-2022 走看看