WebService -- Java 实现之 CXF ( 添加系统预定义的拦截器)

1. 概述

CXF允许我们在webservice的in/out位置添加拦截器。拦截器有两大分类,一类是系统预定义的;另一类是自定义拦截器。

2. 在server端添加拦截器。

     JaxWsServerFactoryBean wsSvrFactoryBean = new JaxWsServerFactoryBean();
        String address = "http://127.0.0.1/helloWorld";
        wsSvrFactoryBean.setAddress(address);
        wsSvrFactoryBean.setServiceClass(HelloWorld.class);
        HelloWorld implementor = new HelloWorldImpl();
        wsSvrFactoryBean.setServiceBean(implementor);
        // add in out logging Interceptors
        wsSvrFactoryBean.getInInterceptors().add(new LoggingInInterceptor());
        wsSvrFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
        wsSvrFactoryBean.create();

3. 在client端添加拦截器。

3.1 在client项目中添加CXF引用

<dependencies>
    <dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-core</artifactId>
		<version>3.1.5</version>
	</dependency>

  	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-frontend-jaxws</artifactId>
		<version>3.1.5</version>
	</dependency>

	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-transports-http-jetty</artifactId>
		<version>3.1.5</version>
	</dependency>
  </dependencies>

3.2 使用ClientProxy对象添加拦截器

HelloWorld helloWorldClient = new HelloWorldService().getHelloWorldPort();
// add on inteceptors
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(helloWorldClient);
client.getInInterceptors().add(new LoggingInInterceptor());
client.getOutInterceptors().add(new LoggingOutInterceptor());

4. client/server output information

--client--

十二月 30, 2016 11:25:45 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://webservice.tuo.example.com/}HelloWorldService from WSDL: http://127.0.0.1/helloWorld?wsdl
十二月 30, 2016 11:25:46 下午 org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld
信息: Outbound Message
---------------------------
ID: 1
Address: http://127.0.0.1/helloWorld
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://webservice.tuo.example.com/"><arg0>Tony</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
十二月 30, 2016 11:25:47 下午 org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld
信息: 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=[Fri, 30 Dec 2016 15:25:46 GMT], Server=[Jetty(9.2.11.v20150529)], transfer-encoding=[chunked]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://webservice.tuo.example.com/"><return>Hello world,Tony</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------
Hello world,Tony

--server--

信息: Inbound Message
----------------------------
ID: 1
Address: http://127.0.0.1/helloWorld?wsdl
Http-Method: GET
Content-Type: text/xml
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], content-type=[text/xml], Host=[127.0.0.1], Pragma=[no-cache], User-Agent=[Apache CXF 3.1.5]}
--------------------------------------
十二月 30, 2016 11:25:46 下午 org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld
信息: Inbound Message
----------------------------
ID: 2
Address: http://127.0.0.1/helloWorld
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[202], content-type=[text/xml; charset=UTF-8], Host=[127.0.0.1], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 3.1.5]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://webservice.tuo.example.com/"><arg0>Tony</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
十二月 30, 2016 11:25:47 下午 org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld
信息: 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://webservice.tuo.example.com/"><return>Hello world,Tony</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------

  

时间: 2024-08-04 18:15:39

WebService -- Java 实现之 CXF ( 添加系统预定义的拦截器)的相关文章

.NET中那些所谓的新语法之三:系统预定义委托与Lambda表达式

开篇:在上一篇中,我们了解了匿名类.匿名方法与扩展方法等所谓的新语法,这一篇我们继续征程,看看系统预定义委托(Action/Func/Predicate)和超爱的Lambda表达式.为了方便码农们,.Net基类库针对实际开发中最常用的情形提供了几个预定义好的委托,这些委托可以直接使用,无需再重头定义一个自己的委托类型.预定义委托在.Net基类库中使用的比较广泛,比如在Lambda表达式和并行计算中都大量地使用,需要我们予以关注起来! /* 新语法索引 */ 1.自动属性 Auto-Impleme

SendMessage函数与MSDN系统预定义消息

SendMessage function https://msdn.microsoft.com/en-us/library/windows/desktop/ms644950%28v=vs.85%29.aspx 系统预定义消息 https://msdn.microsoft.com/en-us/library/windows/desktop/ms644927%28v=vs.85%29.aspx#system_defined 使复选框按钮选中 #define BM_SETCHECK        0x

rest-Assured-解析json错误-需使用预定义的解析器解析

报错信息: java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but content-type 'text/plain' is not supported out of the box.Try registering a custom parser using: RestAssured.registerParser("text/plain", <p

WebService -- Java 实现之 CXF ( 使用:Spring+CXF+Tomcat发布webService)

1. 新建一个Maven项目,选择webapp模板,命名为WS_Spring_CXF_Tomcat 2. 在POM.xml中添加Spring和CXF的依赖 <!-- 添加 Spring dependency --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.7.R

C# 系统预定义的委托EventHandler

.net 预定义了两个委托EventHandler如下. // 表示将用于处理不具有事件数据的事件的方法. // // 参数: // sender: // 事件源. // // e: // 不包含事件数据的对象. [ComVisible(true)] public delegate void EventHandler(object sender, EventArgs e); // // e: // 包含事件数据的对象. // // 类型参数: // TEventArgs: // 事件生成的事件数

WebService -- Java 实现之 CXF ( 使用CXF工具生成client 程序)

1. 下载CXF 工具解压到磁盘 2.添加工具bin目录到PATH环境变量 3.创建一个CXF client新项目 4. run -> cmd 到指定目录,并运行工具目录下的批处理 “wadl2java.bat” 5. 完成后,在IDE中刷新项目,就会发现新生成的包+文件 6. 编写client类,访问webserivce package com.example.tuo.myCXFWebService.WS_Client; import com.example.tuo.webservice.He

WebService -- Java 实现之 CXF (初体验)

1. 认识WebService 简而言之,她就是:一种跨编程语言以及操作系统的远程调用技术. 大家都可以根据定义好的规范和接口进行开发,尽管各自的使用的开发语言和操作系统有所不同,但是由于都遵循统一的规范还有接口,因而可以做到透明和正常交互. 2. CXF 官方主页:http://cxf.apache.org/ 定义: Apache CXF is an open source services framework. CXF helps you build and develop services

WebService -- Java 实现之 CXF (WebService 服务器端接口)

1. 使用Maven创建一个quickstart项目 2. 引入依赖的Jar包 <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.1.5</version> </dependency> <dependency> <groupId>org.apache.cx

Matlab——系统预定义的变量 常用数学函数

原文地址:https://www.cnblogs.com/expedition/p/10884591.html