CXF+spring 部署及调用

本例子使用的是 cxf 2.5
* 依赖包
lib/cxf-2.5.10.jar
lib/wsdl4j-1.6.3.jar
lib/xmlschema-core-2.0.3.jar
lib/neethi-3.0.2.jar
lib/geronimo-servlet_2.5_spec-1.1.2.jar
lib/woodstox-core-asl-4.2.0.jar

lib/stax2-api-3.1.1.jar


1、接口

package com.dotoyo.pmes.ws_cxf_service;

import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

import com.dotoyo.pmes.ws_cxf_service.bean.Reader;

@WebService
public interface IReaderService {
	public String getReader(@WebParam(name = "name") String name,
			@WebParam(name = "password") String password);

	public List<Reader> getReaders();
}
2、实现类
package com.dotoyo.pmes.ws_cxf_service;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

import com.dotoyo.pmes.ws_cxf_service.bean.Reader;

@WebService(endpointInterface = "com.dotoyo.pmes.ws_cxf_service.IReaderService", serviceName = "readerService")
public class ReaderService implements IReaderService {
	public String getReader(@WebParam(name = "name") String name,
			@WebParam(name = "password") String password) {
		return "name:" + name + "  password:" + password;
	}

	public List<Reader> getReaders() {
		List<Reader> readerList = new ArrayList<Reader>();
		readerList.add(new Reader("shun1", "123"));
		readerList.add(new Reader("shun2", "123"));
		return readerList;
	}
}


实体类:
package com.dotoyo.pmes.ws_cxf_service.bean;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "reader")
@XmlAccessorType(XmlAccessType.FIELD)
public class Reader {
	private static final long serialVersionUID = 1L;
	private String name;
	private String password;

	public Reader() {
	}

	public Reader(String name, String password) {
		this.name = name;
		this.password = password;
	}

	// Get/Set方法省略
	public String toString() {
		return "Name:" + name + ",Password:" + password;
	}

}



3、web.xml中的cxf配置
	<!-- cxf的配置 -->
	<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>

4、application中的配置(spring)

<?xml version="1.0" encoding="UTF-8"?>
<beans 
	增加
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
	增加
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<!-- cxf提供的webService -->
	<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="readerServicce"
		implementor="com.dotoyo.pmes.ws_cxf_service.ReaderService" address="/readerService" />

</beans>


--------------------------调用-----------------------------


1、客户端采用cxf调用
在环境变量中配置
CXF_HOME="D:\develop\apache-cxf-2.5.10"
PATH="D:\develop\apache-cxf-2.5.10\bin"

D:\>wsdl2java -p com.test.webService -client wsdl地址


-p  打包
-client  生成测试客户端


public class TestClient {
public static void main(String[] args) {
ReaderService rs = new ReaderService();
IReaderService irs = rs.getReaderServicePort();
String str = irs.getReader("张三", "000");
System.out.println(str);

List<Reader> rList = irs.getReaders();
for(Reader r : rList){
System.out.println(r.getName()+" "+r.getPassword());
}


}
}



2、客户端采用axis1调用
eclipse 中 new  一个  Web Service Client项目


生成代码后调用


public class TestClient {
public static void main(String[] args) throws Exception {
ReaderService rs = new ReaderServiceLocator();
IReaderService  irs = rs.getReaderServicePort();
String result = irs.getReader("admin", "1234");
System.out.println(result);
}

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值