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。
CXF 自定义拦截器的更多相关文章
- Apache CXF自定义拦截器
为什么设计拦截器?1.为了在webservice请求过程中,能动态操作请求和响应数据,CXF设计了拦截器 拦截器分类: 1.按所处的位置分:服务器端拦截器,客户端拦截器. 2.按消息的方向分:入拦截器 ...
- 【WebService】WebService之CXF的拦截器(五)
CXF拦截器介绍 CXF拦截器是功能的主要实现单元,也是主要的扩展点,可以在不对核心模块进行修改的情况下,动态添加功能.当服务被调用时,会经过多个拦截器链(Interceptor Chain)处理,拦 ...
- CXF添加拦截器和自定义拦截器
前面讲了如何采用CXF开发webservice,现在来讲如何添加拦截器和自定义拦截器. 服务端代码: HelloWorld implementor=new HelloWorldImpl(); Stri ...
- (六)CXF之自定义拦截器
一.需求分析 客户端在调用服务端的方法时,需要进行用户名和密码验证.此时分为: 客户端请求的时候,要发送用户名密码到服务端 服务端检验用户名密码. 二.案例 前提:本章案例是基于前一章节的例子进一步讲 ...
- (八)CXF添加自定义拦截器
前面我们说到CXF添加内置的拦截器,今天的话,我们来讲下如何添加自定义拦截器: 我们的实例是客户端访问服务端webservice接口要加权限认证. 我们思路先说下.我们可以通过在SOAP消息的Head ...
- 【CXF】- 拦截器 Interceptor
CXF拦截器 拦截动态操作请求和响应数据 拦截器分类 位置:服务器端拦截器,客户端拦截器 消息方向:入拦截器 出拦截器 定义者:系统拦截器 自定义拦截器:LoggingInInteceptor ①:创 ...
- spring mvc <mvc:annotation-driven/> 自定义拦截器不走
<mvc:annotation-driven/> 这个便签会注册2个自定义拦截器,所以导致请求过来就会自己去走注册的这2个拦截器和定义的一堆bean 但是这个便签是必须得定义的 直接贴代码 ...
- 12.Struts2自定义拦截器
12.自定义拦截器 拦截器是Struts2的一个重要特性.因为Struts2的大多数核心功能都是通过拦截器实现的. 拦截器之所以称之为“拦截器”,是因为它可以拦截Action方法的执行, ...
- 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】
一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...
随机推荐
- 【C++】array初始化0
让代码...优雅? ==================分割线==================== 局部数组:没有默认值,如果声明的时候不定义,则会出现随机数(undefined):如果声明的长度 ...
- Linux的交叉编译 及configure配置
这两天需要把一个CDVS的工程代码从Linux 平台上移植到ARM平台上,花了两天才搞定,之前很早申请的博客,到现在还没有技术文章会,以后决定凡是花两三天才搞定的东西都会把解决过程发到这里,很多东西靠 ...
- codeforces 477A A. Dreamoon and Sums(数学)
题目链接: A. Dreamoon and Sums time limit per test 1.5 seconds memory limit per test 256 megabytes input ...
- map学习笔记
collection是单列集合,map是双列集合.其中包含<k,v>键值对,注意:键具有唯一性,而值不唯一. 在此列举三个读取方式:keyset,valueset,及entryset. k ...
- 栈和队列的面试题Java实现【重要】
栈和队列: 面试的时候,栈和队列经常会成对出现来考察.本文包含栈和队列的如下考试内容: (1)栈的创建 (2)队列的创建 (3)两个栈实现一个队列 (4)两个队列实现一个栈 (5)设计含最小函数min ...
- 为VS集成IL环境
为VS2012集成IL工具 在之前的版本VS2010中,在VS的安装目录下/Tools/IL Disassembler这个工具(IL中间语言查看器),但是我想直接把它集成在VS2012里使用, 操作方 ...
- C# Reflection BindingFlags
定义包含在搜索中的成员 下列 BindingFlags 筛选标志可用于定义包含在搜索中的成员: 为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.St ...
- AutoIT 实现Firefox上传文件
Firefox浏览器文件上传代码如下: ;upload file Func _UploadFile($file) AutoItSetOption("WinTitleMatchMode&quo ...
- Android 中JNI创建实例
参考文档: http://blog.sina.com.cn/s/blog_a11f64590101924l.html http://www.cnblogs.com/hoys/archive/2010/ ...
- SilverIight数据绑定实例
前台Code <DataGrid Name="DataGrid1" AutoGenerateColumns="False" IsReadOnly=&quo ...