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. 大数据学习——sqoop安装

    1上传  sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz 2解压 .bin__hadoop--alpha.tar.gz 3重命名 .bin__hadoop--al ...

  2. Shell脚本学习指南 [ 第三、四章 ] 查找与替换、文本处理工具

    摘要:第三章讨论的是编写Shell脚本时经常用到的两个基本操作.第四章总共介绍了约30种处理文本文件的好用工具. 第三章 查找与替换 概括:本章讨论的是编写Shell脚本时经常用到的两个基本操作:文本 ...

  3. 【Luogu】P1072Hankson的趣味题(gcd)

    这题真TM的趣味. 可以说我的动手能力还是不行,想到了算法却写不出来.以后说自己数论会GCD的时候只好虚了…… 我们首先这么想. x与a0的最大公约数为a1,那么我们把x/=a1,a0/=a1之后,x ...

  4. haskell 乱搞笔记[原创]

        脑洞时间:为什么世界上有那么多程序语言,那是腐朽的资本主义为了增加广大人民学习成本以及编译原理太过普及造成的,建议大学取消编译原理的一切课程,并挥起奥姆休的剃刀,把所有程序语言统统踢了,除机器 ...

  5. Spring-IOC源码解读3-依赖注入

    当容器已经载入了BeanDefinition的信息完成了初始化,我们继续分析依赖注入的原理,需要注意的是依赖注入是用户第一次向IOC容器获取Bean的时候发生的,这里有个例外,那就是如果用户在Bean ...

  6. 使用反射获取类中的属性(可用于动态返回PO类的列,当做表格的表头)

    //利用反射取类中的属性字段 try { Class clazz = Class.forName("houji.bean.model.TaskModel"); Field[] fi ...

  7. 洛谷 [P2953] 牛的数字游戏

    SG搜索 n的范围在可以接受的范围内,SG搜索即可 #include <iostream> #include <cstdio> #include <cstring> ...

  8. BZOJ4723: [POI2017]Flappy Bird

    $n \leq 500000$个水管,每秒横坐标加一,纵坐标如果你点击就+1否则-1,问从$(0,0)$飞到$m$处最少点多少次,或者说明无解. 如果能飞到某个水管的高度区间$[L,R]$,那么答案肯 ...

  9. 频繁项挖掘算法Apriori和FGrowth

    一:背景介绍 最近在公司用spark的平台做了一个购物车的推荐,用到的算法主要是FGrowth算法,它是Apriori算法的升级版,算法的主要目的是找出频繁进行一起购买的商品.本文主要介绍两个算法的背 ...

  10. MongoDB 复制(副本集)学习

    MongoDB 复制(副本集)学习 replication set复制集,复制集,多台服务器维护相同的数据副本,提高服务器的可用性.MongoDB复制是将数据同步在多个服务器的过程.复制提供了数据的冗 ...