CXF发布在Web服务
1、下载apache-cxf-3.1.4,将jar引入新工程中。
2、People.java
package com.soap.server; 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;
}
}
3、PeopleService.java
package com.soap.server; import java.util.List;
import javax.jws.WebService; @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、PeopleServiceImpl.java
package com.soap.server; import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jws.WebService; @WebService(endpointInterface="com.soap.server.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.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>SpringCXFServlet</servlet-name>
<servlet-class>com.cxf.servlet.MySpringCXFNonSpringServlet</servlet-class>
<init-param>
<param-name>/peopleService</param-name>
<param-value>com.soap.server.PeopleServiceImpl</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringCXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
6、写一个类覆盖org.apache.cxf.transport.servlet.CXFNonSpringServlet的loadBus方法指定BUS
package com.cxf.servlet; import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet; public class MySpringCXFNonSpringServlet extends CXFNonSpringServlet {
private static final long serialVersionUID = 1930791254280865620L; @Override
public void loadBus(ServletConfig servletConfig) {
super.loadBus(servletConfig);
Bus bus = this.getBus();
BusFactory.setDefaultBus(bus);
// 获取在web.xml中配置的要发布的所有的Web服务实现类并发布Web服务
Enumeration<String> enumeration = getInitParameterNames();
while (enumeration.hasMoreElements()) {
String key = enumeration.nextElement();
String value = getInitParameter(key);
try {
Class clazz = Class.forName(value);
try {
Endpoint.publish(key, clazz.newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
7、部署工程,若Web应用上下文是/SpringCXFService,
则访问http://127.0.0.1:8080/SpringCXFService/services/peopleService?wsdl
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://server.soap.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="PeopleServiceImplService" targetNamespace="http://server.soap.com/">
- <wsdl:types>
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://server.soap.com/" elementFormDefault="unqualified" targetNamespace="http://server.soap.com/" version="1.0">
<xs:element name="People" type="tns:people" />
<xs:element name="add" type="tns:add" />
<xs:element name="addResponse" type="tns:addResponse" />
<xs:element name="del" type="tns:del" />
<xs:element name="delResponse" type="tns:delResponse" />
<xs:element name="getList" type="tns:getList" />
<xs:element name="getListResponse" type="tns:getListResponse" />
<xs:element name="getOne" type="tns:getOne" />
<xs:element name="getOneResponse" type="tns:getOneResponse" />
<xs:element name="modify" type="tns:modify" />
<xs:element name="modifyResponse" type="tns:modifyResponse" />
- <xs:complexType name="del">
- <xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:people" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="people">
- <xs:sequence>
<xs:element minOccurs="0" name="birthday" type="xs:dateTime" />
<xs:element minOccurs="0" name="id" type="xs:long" />
<xs:element minOccurs="0" name="name" type="xs:string" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="delResponse">
- <xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="add">
- <xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:people" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="addResponse">
- <xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="getOne">
- <xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:long" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="getOneResponse">
- <xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:people" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="getList">
- <xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="getListResponse">
- <xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="tns:people" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="modify">
- <xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:people" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="modifyResponse">
- <xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
- <wsdl:message name="addResponse">
<wsdl:part element="tns:addResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="delResponse">
<wsdl:part element="tns:delResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="getOneResponse">
<wsdl:part element="tns:getOneResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="getOne">
<wsdl:part element="tns:getOne" name="parameters" />
</wsdl:message>
- <wsdl:message name="del">
<wsdl:part element="tns:del" name="parameters" />
</wsdl:message>
- <wsdl:message name="modifyResponse">
<wsdl:part element="tns:modifyResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="getListResponse">
<wsdl:part element="tns:getListResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="getList">
<wsdl:part element="tns:getList" name="parameters" />
</wsdl:message>
- <wsdl:message name="modify">
<wsdl:part element="tns:modify" name="parameters" />
</wsdl:message>
- <wsdl:message name="add">
<wsdl:part element="tns:add" name="parameters" />
</wsdl:message>
- <wsdl:portType name="PeopleService">
- <wsdl:operation name="del">
<wsdl:input message="tns:del" name="del" />
<wsdl:output message="tns:delResponse" name="delResponse" />
</wsdl:operation>
- <wsdl:operation name="add">
<wsdl:input message="tns:add" name="add" />
<wsdl:output message="tns:addResponse" name="addResponse" />
</wsdl:operation>
- <wsdl:operation name="getOne">
<wsdl:input message="tns:getOne" name="getOne" />
<wsdl:output message="tns:getOneResponse" name="getOneResponse" />
</wsdl:operation>
- <wsdl:operation name="getList">
<wsdl:input message="tns:getList" name="getList" />
<wsdl:output message="tns:getListResponse" name="getListResponse" />
</wsdl:operation>
- <wsdl:operation name="modify">
<wsdl:input message="tns:modify" name="modify" />
<wsdl:output message="tns:modifyResponse" name="modifyResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="PeopleServiceImplServiceSoapBinding" type="tns:PeopleService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="add">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="add">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="addResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="del">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="del">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="delResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="getOne">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="getOne">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="getOneResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="getList">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="getList">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="getListResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
- <wsdl:operation name="modify">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="modify">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="modifyResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="PeopleServiceImplService">
- <wsdl:port binding="tns:PeopleServiceImplServiceSoapBinding" name="PeopleServiceImplPort">
<soap:address location="http://127.0.0.1:8080/SpringCXFService/services/peopleService" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
CXF发布在Web服务的更多相关文章
- wcf通过webHttpBinding方式发布rest web服务
<system.serviceModel> <services> <service name="ServiceUpdater.ServiceUpdate&quo ...
- Azure机器学习入门(四)模型发布为Web服务
接Azure机器学习(三)创建Azure机器学习实验,下一步便是真正地将Azure机器学习的预测模型发布为Web服务.要启用Web服务发布任务,首先点击底端导航栏的运行即"Run" ...
- Spring MVC中发布Restful Web服务
对于企业应用来说,数据是许多业务的命脉,软件通常是可替换的,但是多年积累的数据是永远不能替换的. 近些年来,以信息为中心的表述性状态转移(Representational State Tran ...
- Eclipse+CXF框架开发Web服务实战
一. 说明 采用CXF框架开发webservice. 所用软件及版本如下. 操作系统:Window XP SP3. JDK:JDK1.6.0_07,http://www.oracle.com/ ...
- Web服务cxf框架发布2
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任. 作者:永恒の_☆ 地址:http://blog.csdn.net/chenghui0317/ ...
- CXF发布webService服务以及客户端调用
这篇随笔内容是CXF发布webService服务以及客户端调用的方法 CXF是什么? 开发工作之前需要下载CXF和安装 下载地址:http://cxf.apache.org 安装过程: <1&g ...
- 通过CXF方式实现webservice服务
一.CXF的介绍 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 S ...
- SSH集成cxf 发布restful webservice
首先讲一下什么是restful webservice ,这个问题网上一搜有很多博文去长篇大论的介绍它,但是最后你看完了也会觉得云里雾里的,所以我在这里简单的讲一下我理解的rest webservice ...
- 初识Web 服务(即Web Service)
一.什么是Web服务 简单来说就是在Internet上提供的这种服务,我们称这种服务为Web服务. 二.Web服务的主要目标? 是支持跨平台的可互操作性. 三.Web服务的优势 四.Web服务提供了一 ...
随机推荐
- Mac下的Eclipse不能记住工作空间问题
每次启动eclipse都要选择工作空间,即使你勾选了"选择这个作为默认"也不行. Eclipse版本 4.5, mac os版本10.12 找到这个目录下的config.ini文件 ...
- SPOJ - ADALIST,双端队列入门模板!
ADALIST - Ada and List 这道题时限6.5s,激动人心啊,好多人STL一顿乱写AC,哈哈,如果熟悉双端队列的话这道题其实是很水的. 题意:n个数的数列,然后接下来Q次操作,每次可以 ...
- 九度oj 题目1160:放苹果
题目描述: 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. 输入: 第一行是测试数据的数目t(0 <= t ...
- linux删除N天前的文件
列出一天之前的文件的属性用下面的命令:find . ! -mtime -1 -print |xargs ls -lfind . -mtime +1 要删除文件的话用下面的命令:find . ! -mt ...
- 查询UNDO使用情况
查看UNDO事务占用情况 SELECT s.username, s.sid, pr.PID, s.OSUSER, s.MACHINE, s.PROGRAM, rs.segment_id, r.usn, ...
- Error Domain=AVFoundationErrorDomain Code=-11800 "这项操作无法完成"
在iOS上开发视频操作的时候,出现错误: 录制视频错误:Error Domain=AVFoundationErrorDomain Code=-11800 "这项操作无法完成" Us ...
- 【Luogu】P1850换教室(期望DP)
题目链接 又一道面向题解编程的恶心神题.真是叫人质壁分离…… 设f[i][j][k]表示考虑了前i节课,尝试了j次,当前申请结果为k时消耗的体力值. 对于f[i][j][0]有两种情况:一是我们的主角 ...
- BZOJ 4004 [JLOI2015]装备购买 ——线性基
[题目分析] 题目很简单,就是要维护一个实数域上的线性基. 仿照异或空间的线性基的方法,排序之后每次加入一个数即可. 卡精度,开long double 和 1e-6就轻松水过了. [代码] #incl ...
- Wiley出版 SQL Server 2005宝典
原文发布时间为:2008-07-30 -- 来源于本人的百度文章 [由搬家工具导入] Wiley出版 SQL Server 2005宝典 迅雷专用高速下载 thunder://QUFmdHA6L ...
- Redis数据结构之整数集合
整数集合是Redis用于保存整数值的集合抽象数据结构,它可以保存类型为int16_t .int32_t或者int64_t的整数值,并且保证集合中不会出现重复元素. 一.整数集合数据结构定义 参数说明: ...