java1234 webservice 第6 课 cfx与spring整合_自定义拦截器_客户端如

一.只须要在springmvc.xml中添加

<!-- 定义服务提供者 ,也就是定义webservice服务地址! -->
	<!--implementor写入注册的bean  address 访问的地址!  -->
	<jaxws:endpoint implementor="#helloWorld1" address="/HelloWorld">
			<!-- 添加in拦截器 -->
		    <jaxws:inInterceptors>
		         <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
		         <!-- 自定义拦截器 -->
		         <bean class="com.java1234.interceptor.MyInterceptor"/>
		    </jaxws:inInterceptors>
		    <!-- 添加out拦截器 -->
		    <jaxws:outInterceptors>
		         <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
		    </jaxws:outInterceptors>
	 </jaxws:endpoint>

二.访问服务器:

1.生成文件: 运行命令:F:\apache-cxf-3.1.5\bin\wsdl2java  http://127.0.0.1:8080/WS_Spring_CXF/webservice/HelloWorld?wsdljava

2. 新建maven项目,项目结构以下程序员

pox.xml文件web

<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>

client.java方法spring

package com.java1234.webservice;

import java.util.List;

import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

import com.java1234.interceptor.AddHeaderInterceptor;
import com.java1234.webservice.impl.HelloWorldImplService;

public class Client {
	
	 public static void main(String[] args) {
		 
		 //这里web下的服务器和普通main发布的服务器 生成的文件不一样
		 HelloWorldImplService service=new HelloWorldImplService();
		 HelloWorld helloWorld=service.getHelloWorldImplPort();
		//调用的类也有区别

		org.apache.cxf.endpoint.Client client= ClientProxy.getClient(helloWorld);
		client.getOutInterceptors().add(new AddHeaderInterceptor("java1234","123456")); // 添加自定义拦截器		
		client.getInInterceptors().add(new LoggingInInterceptor());//添加
		client.getOutInterceptors().add(new LoggingOutInterceptor());
		

		
		/*
		 *数据只能传输List<T>,不能传输map,因此map的key放入T中,即可以轻松传输! 
		 */
		MyRoleArray arry = helloWorld.getRoles();
		 List<MyRole>   roleList= arry.item;
		
		 for(int i=0; i<roleList.size();i++){
			 
			 MyRole my=roleList.get(i);
			 System.out.println(my.key+":");
			 for(Role r:my.value){
				 System.out.println(r.getId()+","+r.getRoleName());
			 }
			 System.out.println("========================");
		 }
		 
		
		
		
	}

}

 

运行结果:apache

八月 02, 2016 3:22:08 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://impl.webservice.java1234.com/}HelloWorldImplService from WSDL: http://127.0.0.1:8080/WS_Spring_CXF/webservice/HelloWorld?wsdl
八月 02, 2016 3:22:08 下午 org.apache.cxf.services.HelloWorldImplService.HelloWorldImplPort.HelloWorld
信息: Outbound Message
---------------------------
ID: 1
Address: http://127.0.0.1:8080/WS_Spring_CXF/webservice/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:Header><authHeader><userName>java1234</userName><password>123456</password></authHeader></soap:Header><soap:Body><ns2:getRoles xmlns:ns2="http://webservice.java1234.com/"/></soap:Body></soap:Envelope>
--------------------------------------
八月 02, 2016 3:22:08 下午 org.apache.cxf.services.HelloWorldImplService.HelloWorldImplPort.HelloWorld
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[443], content-type=[text/xml;charset=UTF-8], Date=[Tue, 02 Aug 2016 07:22:08 GMT], Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getRolesResponse xmlns:ns2="http://webservice.java1234.com/"><return><item><key>jack</key><value><id>3</id><roleName>程序员</roleName></value></item><item><key>java1234</key><value><id>1</id><roleName>技术总监</roleName></value><value><id>2</id><roleName>架构师</roleName></value></item></return></ns2:getRolesResponse></soap:Body></soap:Envelope>
--------------------------------------
jack:
3,程序员
========================
java1234:
1,技术总监
2,架构师
========================
相关文章
相关标签/搜索