一、下载apache-cxf-3.1.4.zip。
二、编写服务端
1、新建工程,导入apache-cxf-3.1.4.zip下的jar包。
2、编写一个Web Service用来传输参数的类。
package com.ws.services.entity; import javax.xml.bind.annotation.XmlRootElement; /** * 该类为Web Service中的参数、返回值类型,故需要使用JAXB注解告诉CXF如何在XML和Java Object之间处理, * 因为,SOAP消息格式包装的是一段XML代码,无论是服务器端,还是客户端, * 在接收到SOAP消息时,都需要将XML转化为Java Object, * 在发送SOAP消息时,需要将Java Object转化为XML。 * */ @XmlRootElement(name = "People") public class People { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
3、编写Web Service接口。
package com.ws.services; import java.util.List; import javax.jws.WebService; import com.ws.services.entity.People; @WebService public interface PeopleService { public String add(People people); public String del(People people); public String modify(People people); public People getOne(String id); public List<People> getList(String name); }
4、编写Web Service接口的实现类。
package com.ws.services; import java.util.ArrayList; import java.util.List; import javax.jws.WebService; import com.ws.services.entity.People; @WebService(endpointInterface="com.ws.services.PeopleService") public class PeopleServiceImpl implements PeopleService { @Override public String add(People people) { // TODO Auto-generated method stub System.out.println("ADD:"+people.getId()+","+people.getName()); System.out.println("SUCCESS"); return "ADD SUCCESS"; } @Override public String del(People people) { // TODO Auto-generated method stub System.out.println("DEL:"+people.getId()+","+people.getName()); System.out.println("SUCCESS"); return "DEL SUCCESS"; } @Override public String modify(People people) { // TODO Auto-generated method stub System.out.println("MODIFY:"+people.getId()+","+people.getName()); System.out.println("SUCCESS"); return "MODIFY SUCCESS"; } @Override public People getOne(String id){ // TODO Auto-generated method stubSystem.out.println("QRY BEGIN"); People people=new People(); people.setId("004"); people.setName("Name-004"); System.out.println("SUCCESS"); return people; } @Override public List<People> getList(String name){ // TODO Auto-generated method stub List<People> list=new ArrayList<People>(); People people0=new People(); People people1=new People(); people0.setId("005"); people0.setName(name+"-005"); people1.setId("006"); people1.setName(name+"-006"); list.add(people0); list.add(people1); System.out.println("SUCCESS"); return list; } }
5、发布Web Service服务
package com.ws.services; import javax.xml.ws.Endpoint; public class SoapServer { public static void main(String[] args) { // TODO Auto-generated method stub //发布服务 Endpoint.publish("http://127.0.0.1:80/peopleService", new PeopleServiceImpl()); } }
6、测试服务发布情况
运行Java Application,访问http://127.0.0.1/peopleService?wsdl
<?xml version="1.0" encoding="UTF-8" ?> - <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI‘s version is JAX-WS RI 2.2.4-b01. --> - <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI‘s version is JAX-WS RI 2.2.4-b01. --> - <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://services.ws.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://services.ws.com/" name="PeopleServiceImplService"> - <types> - <xsd:schema> <xsd:import namespace="http://services.ws.com/" schemaLocation="http://127.0.0.1/peopleService?xsd=1" /> </xsd:schema> </types> - <message name="add"> <part name="parameters" element="tns:add" /> </message> - <message name="addResponse"> <part name="parameters" element="tns:addResponse" /> </message> - <message name="modify"> <part name="parameters" element="tns:modify" /> </message> - <message name="modifyResponse"> <part name="parameters" element="tns:modifyResponse" /> </message> - <message name="getOne"> <part name="parameters" element="tns:getOne" /> </message> - <message name="getOneResponse"> <part name="parameters" element="tns:getOneResponse" /> </message> - <message name="getList"> <part name="parameters" element="tns:getList" /> </message> - <message name="getListResponse"> <part name="parameters" element="tns:getListResponse" /> </message> - <message name="del"> <part name="parameters" element="tns:del" /> </message> - <message name="delResponse"> <part name="parameters" element="tns:delResponse" /> </message> - <portType name="PeopleService"> - <operation name="add"> <input wsam:Action="http://services.ws.com/PeopleService/addRequest" message="tns:add" /> <output wsam:Action="http://services.ws.com/PeopleService/addResponse" message="tns:addResponse" /> </operation> - <operation name="modify"> <input wsam:Action="http://services.ws.com/PeopleService/modifyRequest" message="tns:modify" /> <output wsam:Action="http://services.ws.com/PeopleService/modifyResponse" message="tns:modifyResponse" /> </operation> - <operation name="getOne"> <input wsam:Action="http://services.ws.com/PeopleService/getOneRequest" message="tns:getOne" /> <output wsam:Action="http://services.ws.com/PeopleService/getOneResponse" message="tns:getOneResponse" /> </operation> - <operation name="getList"> <input wsam:Action="http://services.ws.com/PeopleService/getListRequest" message="tns:getList" /> <output wsam:Action="http://services.ws.com/PeopleService/getListResponse" message="tns:getListResponse" /> </operation> - <operation name="del"> <input wsam:Action="http://services.ws.com/PeopleService/delRequest" message="tns:del" /> <output wsam:Action="http://services.ws.com/PeopleService/delResponse" message="tns:delResponse" /> </operation> </portType> - <binding name="PeopleServiceImplPortBinding" type="tns:PeopleService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> - <operation name="add"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input> - <output> <soap:body use="literal" /> </output> </operation> - <operation name="modify"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input> - <output> <soap:body use="literal" /> </output> </operation> - <operation name="getOne"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input> - <output> <soap:body use="literal" /> </output> </operation> - <operation name="getList"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input> - <output> <soap:body use="literal" /> </output> </operation> - <operation name="del"> <soap:operation soapAction="" /> - <input> <soap:body use="literal" /> </input> - <output> <soap:body use="literal" /> </output> </operation> </binding> - <service name="PeopleServiceImplService"> - <port name="PeopleServiceImplPort" binding="tns:PeopleServiceImplPortBinding"> <soap:address location="http://127.0.0.1/peopleService" /> </port> </service> </definitions>
三、编写客户端代码
1、使用WSDL2Java生成Web Service客户端代码
(1)配置CXF环境变量
path中加入apache-cxf-3.1.4\bin的绝对路径。
(2)使用WSDL2Java生成Web Service客户端代码,命令如下:
wsdl2java -p com.ws.client -d D:\\src -client http://127.0.0.1/peopleService?wsdl
将生成的类拷入Web Service客户端工程中。
2、编写客户端测试方法
package com.ws; import java.text.ParseException; import java.util.List; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.ws.client.People; import com.ws.client.PeopleService; public class SoapClient { public static void main(String[] args) throws ParseException { JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean(); soapFactoryBean.setAddress("http://127.0.0.1/peopleService"); soapFactoryBean.setServiceClass(PeopleService.class); Object o = soapFactoryBean.create(); PeopleService ps = (PeopleService) o; People p1 = new People(); p1.setId("001"); p1.setName("陈一"); ps.add(p1); People p2 = new People(); p2.setId("002"); p2.setName("吕二"); ps.del(p2); People p3 = new People(); p3.setId("003"); p3.setName("张三"); ps.del(p3); People p4=ps.getOne("004"); System.out.println("004:"+p4.getId()+","+p4.getName()); List<People> list=ps.getList("王五"); for(People p:list){ System.out.println(":"+p.getId()+","+p.getName()); } } }
最后,进行测试。
备注:
webservice服务端启动时,报错:prefix wsdp is not bound to a namespace,去掉下列四个jar包,
cxf-services-ws-discovery-api-3.1.4.jar
cxf-services-ws-discovery-service-3.1.4.jar
cxf-services-wsn-api-3.1.4.jar
cxf-services-wsn-core-3.1.4.jar
时间: 2024-10-17 02:13:37