此例子来自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。

CXF 自定义拦截器的更多相关文章

  1. Apache CXF自定义拦截器

    为什么设计拦截器?1.为了在webservice请求过程中,能动态操作请求和响应数据,CXF设计了拦截器 拦截器分类: 1.按所处的位置分:服务器端拦截器,客户端拦截器. 2.按消息的方向分:入拦截器 ...

  2. 【WebService】WebService之CXF的拦截器(五)

    CXF拦截器介绍 CXF拦截器是功能的主要实现单元,也是主要的扩展点,可以在不对核心模块进行修改的情况下,动态添加功能.当服务被调用时,会经过多个拦截器链(Interceptor Chain)处理,拦 ...

  3. CXF添加拦截器和自定义拦截器

    前面讲了如何采用CXF开发webservice,现在来讲如何添加拦截器和自定义拦截器. 服务端代码: HelloWorld implementor=new HelloWorldImpl(); Stri ...

  4. (六)CXF之自定义拦截器

    一.需求分析 客户端在调用服务端的方法时,需要进行用户名和密码验证.此时分为: 客户端请求的时候,要发送用户名密码到服务端 服务端检验用户名密码. 二.案例 前提:本章案例是基于前一章节的例子进一步讲 ...

  5. (八)CXF添加自定义拦截器

    前面我们说到CXF添加内置的拦截器,今天的话,我们来讲下如何添加自定义拦截器: 我们的实例是客户端访问服务端webservice接口要加权限认证. 我们思路先说下.我们可以通过在SOAP消息的Head ...

  6. 【CXF】- 拦截器 Interceptor

    CXF拦截器 拦截动态操作请求和响应数据 拦截器分类 位置:服务器端拦截器,客户端拦截器 消息方向:入拦截器 出拦截器 定义者:系统拦截器 自定义拦截器:LoggingInInteceptor ①:创 ...

  7. spring mvc <mvc:annotation-driven/> 自定义拦截器不走

    <mvc:annotation-driven/> 这个便签会注册2个自定义拦截器,所以导致请求过来就会自己去走注册的这2个拦截器和定义的一堆bean 但是这个便签是必须得定义的 直接贴代码 ...

  8. 12.Struts2自定义拦截器

    12.自定义拦截器        拦截器是Struts2的一个重要特性.因为Struts2的大多数核心功能都是通过拦截器实现的. 拦截器之所以称之为“拦截器”,是因为它可以拦截Action方法的执行, ...

  9. 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】

    一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...

随机推荐

  1. 算法实践——Twitter算法面试题(积水问题)的线性时间解法

    问题描述:在下图里我们有不同高度的挡板.这个图片由一个整数数组所代表,数组中每个数是墙的高度.下图可以表示为数组(2.5.1.2.3.4.7.2).假如开始下雨了,那么挡板之间的水坑能够装多少水(水足 ...

  2. cni 添加网络 流程分析

    cnitool: Add or remove network interfaces from a network namespace cnitool add <net> <netns ...

  3. 使用jsonpath解析json内容

    JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容.下面我把官网介绍的每个表达式用代码实现,可以更直观的知道该怎么用它. 一.首先需要 ...

  4. Tarjian算法求强联通分量

    如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.强连通图有向图的极大强连通子图,称为强连通分量(strong ...

  5. 只有图片拼接的html页面图片之间有白条的解决方法

    有时候会有这样的页面,整个页面也就是几张切好的图片组成,但是把这些图片使用代码拼接好,又总会出现图片间有白条的问题,如下图: 解决方法:给图片的父容器添加 line-height: 0; 就好了,因为 ...

  6. dexDebug ExecException finished with non-zero exit value 2

    Error:Execution failed for task ':app:transformClassesWithDexForDebug'. com.android.build.api.transf ...

  7. [原创]Gerrit中文乱码问题解决方案分享

    应开发同事的要求,部署了Gitlab+Gerrit+Jenkins的持续集成环境. 但是发现了一个问题,Gerrit登陆后有中文乱码出现. 具体情况如下: (1)Git代码中的中文乱码处理: 为妥善解 ...

  8. immutability-javascript

    https://www.sitepoint.com/immutability-javascript/

  9. Linux 守护进程一

    守护进程是一个后台进程,它无需用户输入就能运行,经常是提供某种服务. LInux作为服务器,主要的进程也都是为系统或用户提供后台服务功能. 常见的守护进程有Web服务器.邮件服务器以及数据库服务器等等 ...

  10. angularjs中只显示选中的radio的值

    angularjs中,只显示选中的radio的值.主要是相同的radio,name属性值要相同还有ng-model的值要相同,同时要指定value值.这样选中的时候就会在下面的div中显示选中的值了. ...