为什么设计拦截器?

为了在webservice请求中,可以主动操作请求和相应数据,CXF设计了拦截器。

拦截器分类:

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

2.按照消息的方向划分:入拦截器、出拦截器;

3.按照定义者划分:系统拦截器,自定义拦截器。

拦截器API

拦截器接口:

AbstractPhaseInterceptor

LoggingInInterceptor

LoggingOutInterceptor

那么我们就以日志拦截器为例,直接上代码:

服务端:

package com.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface myWS {

	@WebMethod
	public String sayHello(String name);
	
}
package com.service;

import javax.jws.WebService;

@WebService
public class myWSImpl implements myWS {

	@Override
	public String sayHello(String name) {
		System.out.println("SERVER SAYHELLO!"+name);
		return "hello !"+name;
	}

}
package com.test;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.phase.AbstractPhaseInterceptor;

import com.service.myWSImpl;

public class RRRR {

	public static void main(String[] args) {
		
		String address = "http://localhost:8989/sayHello";

		//发布服务端
		EndpointImpl epi = (EndpointImpl) EndpointImpl.publish(address, new myWSImpl());
		//服务端入拦截器添加一个日志入拦截器
		epi.getInInterceptors().add(new LoggingInInterceptor());
		//服务端出拦截器添加一个日志出拦截器
		epi.getOutInterceptors().add(new LoggingOutInterceptor());
		//发布服务
		System.out.println("WebService 发布成功 , address : " + address);
		
		
	}

}

启动日志:

十一月 25, 2018 8:27:24 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.com/}myWSImplService from class com.service.myWS
十一月 25, 2018 8:27:24 下午 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:8989/sayHello
十一月 25, 2018 8:27:24 下午 org.eclipse.jetty.util.log.Log initialized
信息: Logging initialized @1721ms to org.eclipse.jetty.util.log.Slf4jLog
十一月 25, 2018 8:27:24 下午 org.eclipse.jetty.server.Server doStart
信息: jetty-9.4.12.v20180830; built: 2018-08-30T13:59:14.071Z; git: 27208684755d94a92186989f695db2d7b21ebc51; jvm 1.8.0_131-b11
十一月 25, 2018 8:27:25 下午 org.eclipse.jetty.server.AbstractConnector doStart
信息: Started ServerConnector@67c8a337{HTTP/1.1,[http/1.1]}{localhost:8989}
十一月 25, 2018 8:27:25 下午 org.eclipse.jetty.server.Server doStart
信息: Started @1994ms
十一月 25, 2018 8:27:25 下午 org.eclipse.jetty.server.handler.ContextHandler setContextPath
警告: Empty contextPath
十一月 25, 2018 8:27:25 下午 org.eclipse.jetty.server.handler.ContextHandler doStart
信息: Started o.e.j.s.h.ContextHandler@3403e2ac{/,null,AVAILABLE}
十一月 25, 2018 8:27:25 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl
十一月 25, 2018 8:27:25 下午 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be soap.udp://239.255.255.250:3702
十一月 25, 2018 8:27:25 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImpl
WebService 发布成功 , address : http://localhost:8989/sayHello

客户端:

package com.test;

import java.util.List;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.message.Message;

import com.service.MyWSImpl;
import com.service.MyWSImplService;

public class ClientTest {

	public static void main(String[] args) {
		MyWSImplService myWSImplService=new MyWSImplService();
		MyWSImpl myWSImplPort = myWSImplService.getMyWSImplPort();

		//使用cxf的客户端代理
		Client client = ClientProxy.getClient(myWSImplPort);
		//获取客户端的入拦截器  注意这里的拦截器是一个list
		List<Interceptor<? extends Message>> inInterceptors = client.getInInterceptors();
		//向客户端的入拦截器添加一个入日志拦截器
		inInterceptors.add(new LoggingInInterceptor());


		//同理,添加客户端的出拦截器
		//获取客户端的出拦截器  注意这里的出拦截器是一个list
		List<Interceptor<? extends Message>> outInterceptors = client.getOutInterceptors();
		//向客户端的出拦截器添加一个出日志拦截器
		outInterceptors.add(new LoggingOutInterceptor());
		
		//发送请求
		String res = myWSImplPort.sayHello("张三");
		System.out.println(res);

	}

}

启动日志:

十一月 25, 2018 8:27:41 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://service.com/}myWSImplService from WSDL: http://localhost:8989/sayHello?wsdl
十一月 25, 2018 8:27:41 下午 org.apache.cxf.services.myWSImplService.myWSImplPort.myWS
信息: Outbound Message
---------------------------
ID: 1
Address: http://localhost:8989/sayHello
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://service.com/"><arg0>张三</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
十一月 25, 2018 8:27:41 下午 org.apache.cxf.services.myWSImplService.myWSImplPort.myWS
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=utf-8
Headers: {content-type=[text/xml;charset=utf-8], Date=[Sun, 25 Nov 2018 12:27:41 GMT], Server=[Jetty(9.4.12.v20180830)], transfer-encoding=[chunked]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://service.com/"><return>hello !张三</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------
hello !张三

服务端交互日志

十一月 25, 2018 8:27:40 下午 org.apache.cxf.services.myWSImplService.myWSImplPort.myWS
信息: Inbound Message
----------------------------
ID: 1
Address: http://localhost:8989/sayHello?wsdl
Http-Method: GET
Content-Type: 
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Type=[null], Host=[localhost:8989], Pragma=[no-cache], User-Agent=[Apache-CXF/3.2.7]}
--------------------------------------
十一月 25, 2018 8:27:41 下午 org.apache.cxf.services.myWSImplService.myWSImplPort.myWS
信息: Inbound Message
----------------------------
ID: 2
Address: http://localhost:8989/sayHello
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[194], content-type=[text/xml; charset=UTF-8], Host=[localhost:8989], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache-CXF/3.2.7]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://service.com/"><arg0>张三</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
SERVER SAYHELLO!张三
十一月 25, 2018 8:27:41 下午 org.apache.cxf.services.myWSImplService.myWSImplPort.myWS
信息: Outbound Message
---------------------------
ID: 2
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://service.com/"><return>hello !张三</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------

可以通过日志看出服务端和客户端交互的数据。

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐