JAVA的WebService规范JAX-WS
JAX-WS的服务端、客户端双方传输数据使用的SOAP消息格式封装数据。
一、下载apache-cxf-3.1.4.zip。
二、编写服务端
1、编写一个Web Service用来传输参数的类
package com.ws.services.entity; import java.util.Date; 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 Long id; private String name; private Date birthday; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
2、编写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(Long id); public List<People> getList(String name); }
4、实现Web Service
package com.ws.services; import java.util.ArrayList; import java.util.Date; 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()+","+people.getBirthday()); return "ADD SUCCESS"; } @Override public String del(People people) { // TODO Auto-generated method stub System.out.println("DEL:"+people.getId()+","+people.getName()); return "DEL SUCCESS"; } @Override public String modify(People people) { // TODO Auto-generated method stub System.out.println("MODIFY:"+people.getId()+","+people.getName()); return "MODIFY SUCCESS"; } @Override public People getOne(Long id){ // TODO Auto-generated method stubSystem.out.println("QRY BEGIN"); People people=new People(); people.setId(4L); people.setName("Name-004"); people.setBirthday(new Date()); 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(5L); people0.setName(name+"-005"); people0.setBirthday(new Date()); people1.setId(6L); people1.setName(name+"-006"); people1.setBirthday(new Date()); list.add(people0); list.add(people1); return list; } }
5、发布Web Service服务
package com.ws.services; import javax.xml.ws.Endpoint; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class SoapServer { public static void main(String[] args) { // TODO Auto-generated method stub /** * 方法一,使用javax.xml.ws.*包中的EndPoint的静态方法publish()发布Web服务 * */ //Endpoint.publish("http://127.0.0.1:80/peopleService", new PeopleServiceImpl()); /** * 方法二,使用CXF特有的API---JaxWsServerFactoryBean发布Web服务, * 并且我们对服务端工厂Bean的输入拦截器集合、输出拦截器集合中分别添加了日志拦截器, * 可以在Web服务端发送和接收消息时输出信息。 */ JaxWsServerFactoryBean soapFactoryBean = new JaxWsServerFactoryBean(); soapFactoryBean.getInInterceptors().add(new LoggingInInterceptor()); soapFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); // 注意这里是实现类不是接口 soapFactoryBean.setServiceClass(PeopleServiceImpl.class); soapFactoryBean.setAddress("http://127.0.0.1:80/peopleService"); soapFactoryBean.create(); System.out.println("published..."); } }
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.net.MalformedURLException; import java.net.URL; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.GregorianCalendar; import java.util.List; import javax.xml.namespace.QName; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl; import com.ws.client.People; import com.ws.client.PeopleService; import com.ws.client.PeopleServiceImplService; public class SoapClient { public static void main(String[] args) throws ParseException, MalformedURLException { // TODO Auto-generated method stub /** * 使用准的JAX-WS的API完成客户端调用 * */ //使用Web服务的WSDL中的targetNamespace和<wsdl:service …中的name属性构建了javax.xml.namespace.QName接口 QName qName = new QName("http://services.ws.com/", "PeopleServiceImplService"); PeopleServiceImplService peopleServiceImplService = new PeopleServiceImplService(new URL("http://127.0.0.1/peopleService?wsdl"), qName); PeopleService ps = (PeopleService) peopleServiceImplService.getPort(PeopleService.class); /** * 使用CXF 的JaxWsProxyFactoryBean来完成客户端调用 * */ /*JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean(); soapFactoryBean.setAddress("http://127.0.0.1:80/peopleService"); soapFactoryBean.setServiceClass(PeopleService.class); Object o = soapFactoryBean.create(); PeopleService ps = (PeopleService) o;*/ People p1 = new People(); p1.setId(1L); p1.setName("陈一"); GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance(); calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("1989-01-28")); p1.setBirthday(new XMLGregorianCalendarImpl(calendar)); ps.add(p1); People p4 = ps.getOne(4L); System.out.println("4:" + p4.getId() + "," + p4.getName() + "," + p4.getBirthday()); List<People> p5 = ps.getList("王五"); for (People p : p5) { System.out.println("5:" + p.getId() + "," + p.getName() + "," + p.getBirthday()); } } }
最后,进行测试。
备注:
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