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。




     

  • 相关阅读:
    编程经验
    GIS业务逻辑
    算法逻辑
    js中arguments的作用
    C#基础及记忆概念
    POI创建Excle
    数据库元数据分析Demo
    c3p0写连接池 Demo
    dbutils的使用Demo
    dbcp写连接池 Demo
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986848.html
Copyright © 2011-2022 走看看