开发webservice服务端和客户端(cxf,xfire)

一、CXF的介绍

Apache CXF是一个开源的WebService框架,CXF大大简化了Webservice的创建,同时它继承了XFire的传统,一样可以和spring天然的进行无缝的集成。CXF框架是一种基于servlet技术的SOA应用开发框架,要正常运用基于CXF应用框架开发的企业应用,除了CXF应用本身之外,还需要JDK和servlet容器的支持。

service文档结构:

二、CXF的准备条件

下载地址:http://download.csdn.net/detail/lovebosom/9649110 可以挑选所需的jar

 

三、创建webservice服务端

1在web.xml中添加cxf的核心servlet

 

 	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/webservice/*</url-pattern>
	</servlet-mapping>

 

2在applicationContext.xml中配置webservice服务

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
		<import resource="classpath:META-INF/cxf/cxf.xml" />
		<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
		<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	<jaxws:endpoint
		id="getInfo" implementor="com.zhan.webservice.inter.GetInfoImpl"
		address="/getInfo" />
	
</beans>

3 定义webservice接口和实现类。

@WebService
public interface GetInfo {
	
	/*发现有时不用@WebParam也可以接收到参数*/
	public void sayHello(@WebParam(name="userName") String userName,@WebParam(name="age") int age);	
}


@WebService(targetNamespace="http://inter.webservice.zhan.com/",
endpointInterface="com.zhan.webservice.inter.GetInfo")
public class GetInfoImpl implements GetInfo {

	public GetInfoImpl() {
		System.out.println("service GetInfoImpl()..");
	}

	@Override
	public String sayHello(String name,int age) {
  		System.out.println("service sayHello()..."+ name +",年龄:"+age);
 		return "hello:"+name +"年龄:"+age;
 	}
}



 4 保存代码,启动tomcat,发布项目。

地址栏输入:http://localhost:8080/webservice_cxf_server/webservice/getInfo?wsdl 即可看到文档

四、创建webservice客户端

1、新建java工程:webservice_cxf_client测试代码如下:

 

import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class ClientTest {

	public static void main(String[] args) {
		JaxWsDynamicClientFactory cf = JaxWsDynamicClientFactory.newInstance();
		String wsurl = "http://127.0.0.1:8080//webservice_cxf_server/webservice/getInfo?wsdl";
		Client client = cf.createClient(wsurl);
		String namespace = "http://inter.webservice.zhan.com/";
		String method="sayHello";
		 try {
			/**
			 * cxf方式 : 2种方式
			 */
			 //第1种方式:不使用命名空间,直接使用方法名
			Object[] objects=client.invoke(method,"cxf1",11);//方法名,参数
			System.out.println("=="+objects[0].toString());
			
			//第2种方式:使用命名空间,使用QName对象(QName+方法名)+参数,
			QName qname = new QName("http://inter.webservice.zhan.com/", method);//(namespace,method)
			Object[] objects2=client.invoke(qname,"cxf2",22);//QName,参数
			System.out.println(objects2[0]); 
			
			/**
			 * axis 调用webservice
			 */
			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(wsurl);//wsurl换为new URL(wsurl)也可以
			call.setOperation(method);// 要调用的方法名
			call.setUseSOAPAction(true);
			call.setOperationName(new QName(namespace,method));
			call.addParameter(
					"userName",//如果服务端收不到参数,可以试试 new QName(namespace,"userName"), 
					org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
			call.addParameter("age",//如果服务端收不到参数,可以试试 new QName(namespace,"age"), 
					org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
			call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
			String str = (String) call.invoke(new Object[]{"axis",33});
			System.out.println(str);
			
		} catch (Exception e) {
			e.printStackTrace();
		}   
		 
	}

}



 

2控制台打印的结果:

 

更多webservice创建和调用方式:http://download.csdn.net/download/lovebosom/9649112


二、xfire开发webservice

1.web.xml 配置:xfire

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext.xml,
		        classpath:xfire-servlet.xml
		</param-value>
	</context-param>
	<servlet>
		<servlet-name>XFireServlet</servlet-name>
		<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>XFireServlet</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>

2.配置xfire-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	
	
	<!-- http://127.0.0.1/gx/services/LearnerService?wsdl   LearnerService为接口名,不知道为啥用lsService就是不行-->
	<bean name="lsService" class="org.codehaus.xfire.spring.ServiceBean">
		<property name="serviceBean" ref="lservice" /><!-- lservice为实现类 -->
		<property name="serviceClass" value="com.myland.webservice.service.LearnerService" />
		<property name="inHandlers">
			<list>
				<ref bean="addressingHandler"/>
			</list>
		</property>
	</bean>
	<bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler" />
</beans>

3.编写接口和实现类:

/**
 * 删除学员信息
 * @author gyz
 * 2017年11月3日 上午11:28:12
 *
 */
public interface LearnerService {
 
  public String deleteLearner(String icCard,String area);
  
}

public class LearnerServiceImpl   implements LearnerService {
 
 private static final Logger log = Logger.getLogger(LearnerServiceImpl.class);
 
 @SuppressWarnings("unchecked")
 public String deleteLearner(String icCard, String area) {

  //TODO 。。。

4.http://127.0.0.1/gx/services/LearnerService?wsdl.....打开。

5.客户端采用cxf调用:

public static void main(String[] args) throws Exception {
  
  JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
   Client client = dcf.createClient("http://127.0.0.1/gx/services/LearnerService?wsdl");
   Object[] objects = client.invoke("deleteLearner", new Object[]{"24354","450800"});
   System.out.println("Echo response: "+ objects[0]);



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猩猩之火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值