* 依赖包
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);
}
}