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

JAVA的WebService规范JAX-WS的更多相关文章

  1. java 调用webservice的各种方法总结

    java 调用webservice的各种方法总结 几种流行的开源WebService框架Axis1,Axis2,Xfire,CXF,JWS比较 方法一:创建基于JAX-WS的webservice(包括 ...

  2. C# 开发XML Web Service与Java开发WebService

    一.web service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量 ...

  3. C#调用Java的WebService添加SOAPHeader验证(2)

    C#调用Java的WebService添加SOAPHeader验证 上一篇链接如上,更像是 Net下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) 来处理xml, ...

  4. WebService—规范介绍和几种实现WebService的框架介绍

    一.关于SOA(面向服务架构)思想   1.关于协议   2.SOA 的诞生 SOA(Service-Oriented Architecture)面向服务架构是一种思想,它将应用程序的不同功能单元通过 ...

  5. Java创建WebService服务及客户端实现(转)

    简介 WebService是一种服务的提供方式,通过WebService,不同应用间相互间调用变的很方便,网络上有很多常用的WebService服务,如:http://developer.51cto. ...

  6. Java调用webservice接口方法

                             java调用webservice接口   webservice的 发布一般都是使用WSDL(web service descriptive langu ...

  7. Java创建WebService服务及客户端实现

    简介 WebService是一种服务的提供方式,通过WebService,不同应用间相互间调用变的很方便,网络上有很多常用的WebService服务,如:http://developer.51cto. ...

  8. java的WebService实践(cxf)

    Java发布WebService,结合Spring,通过cxf的方式 难点:1.引用什么jar包: 1.创建接口 源码如下: package com.nankang; import javax.jws ...

  9. Java之webService知识

    Java之webService知识 1 webservice基础知识 1.1 webService请求的本质 一次webService本质请求,如下所示: 1.2 wsdl文档解析 wsdl文档元素结 ...

随机推荐

  1. asp.net下js调用session

    大致方法为:js调用webservise,然后通过webservise将session值返回给js完成调用 其实最主要的一点就是在webmethod中允许session:[WebMethod(Enab ...

  2. python基础-面向对象(装饰器)

    属性:   @property   @method_name.setter   @method_name.deleter   三个标签都是放在方法的上面来使用,且方法名要和后续使用的   变量名字相一 ...

  3. [uiautomator篇]uiwatcher 的使用场景

    http://www.yangyanxing.com/article/use-watcher-in-uiautomator.html   今天在uiautomator中实践了watcher的用法,这个 ...

  4. 2014·NOIP 新的历程,新的开始

    10.12 从9月1号开始奋战,到了今天终于重新把所有普及和提高的复赛题全部AC了.40多天AC130多道想想也是醉了,也许是机房一群大神给予我的压力吧. 想想暑假,整天玩游戏,与此同时,CZL.CY ...

  5. 算法复习——有源汇上下界可行流(bzoj2396)

    题目: Description We are supposed to make a budget proposal for this multi-site competition. The budge ...

  6. 刷题总结——路径(ssoi)

    题目: 题目背景 CF 57D 题目描述 小美今天和她的好朋友在玩捉迷藏游戏.地图可以抽象成一张 n*m 的图,地图上有一些障碍.但这些障碍有一些性质:1:每个障碍周围 8 个格子是没有障碍的.2:每 ...

  7. 中国余数定理 1(codevs 3040)

    题目描述 Description 摘自算法导论...... 找出第k个被3,5,7除的时候,余数为2,3,2的数: 输入描述 Input Description 一个数k. 输出描述 Output D ...

  8. POJ 2002 Squares [hash]

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 16631   Accepted: 6328 Descript ...

  9. CentOS 7 使用iptables 开放端口

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service system ...

  10. Day 2 操作系统基础

    课前复习新知识 RAID(Redundant Arrays of Independent Disks)独立冗余磁盘阵列 定义:加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵 ...