微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService学习笔记-CXF添加拦截器

Webservice拦截器:在webservice请求过程中,动态操作请求和响应的数据

分类

按照所处的位置分:服务器端拦截器  客户端拦截

按照消息的方向分:入拦截器  出拦截

按照定义者分:系统拦截自定义拦截器 



在服务器端添加拦截

package com.demo;

//注意引入的类一定要正确

import javax.xml.ws.Endpoint;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;

public class webServiceApp {
	public static void main(String[] args) {
		System.out.println("Starting web service... ");
		HelloWorldImpl implementor = new HelloWorldImpl();
		String address = "http://localhost:8080/helloWorld";
		Endpoint endpoint = Endpoint.publish(address, implementor);

		// jaxws API 转到 cxf API 添加日志拦截器
		EndpointImpl jaxwsEndpointImpl = (EndpointImpl) endpoint;
		org.apache.cxf.endpoint.Server server = jaxwsEndpointImpl.getServer();
		org.apache.cxf.endpoint.Endpoint cxfEndpoint = server.getEndpoint();

		LoggingInInterceptor logging = new LoggingInInterceptor();
		cxfEndpoint.getininterceptors().add(logging);
		System.out.println("Web service started");
	}
}


客户端

package com.demo.client;

import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyfactorybean;

import com.demo.HelloWorld;
import com.demo.User;

//参考http://blog.csdn.net/fhd001/article/details/5778915
public class HelloWorldClient {
	public static void main(String[] args) {
		JaxWsProxyfactorybean svr = new JaxWsProxyfactorybean();
		svr.setServiceClass(HelloWorld.class);
		svr.setAddress("http://localhost:8080/helloWorld");
		HelloWorld hw = (HelloWorld) svr.create();

		// jaxws API 转到 cxf API 添加日志拦截器
		org.apache.cxf.endpoint.Client client = org.apache.cxf.frontend.ClientProxy
				.getClient(hw);
		org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();

		LoggingOutInterceptor logging = new LoggingOutInterceptor();
		cxfEndpoint.getoutInterceptors().add(logging);

		User user = new User();
		user.setUsername("Umgsai");
		user.setDescription("test");
		System.out.println(hw.sayHiToUser(user));
		String sayHi = hw.sayHi("test~~~");
		System.out.println(sayHi);
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐