<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
使用Spring+CXF
开发
WebService
Apache CXF 提供方便的
Spring
整合方法,可以通过注解、
Spring
标签式配置来暴露
Web Services
和消费
Web Services
各种类型的Annotation
。
@WebService
和
@WebMethod
是
WSDL
映射
Annatotion
。这些
Annotation
将描述
Web Service
的
WSDL
文档元素和
Java
源代码联系在一起。
@SOAPBinding
是一个绑定的
annotation
用来说明网络协议和格式。
1、
@WebService annotation
的元素
name,serviceName
和
targetNamespace
成员用来描述
wsdl:portType,
wsdl:service
,和
targetNameSpace
生成
WebService
中的
WSDL
文件。
2、
@SOAPBinding
是一个用来描述
SOAP
格式和
RPC
的协议的绑定
Annotation
。
3、
@WebMethod Annotation
的
operationName
成员描述了
wsdl:operation
,而且它的操作描
述了WSDL
文档中的
SOAPAction
头部。这是客户端必须要放入到
SQAPHeader
中的数
值,SOAP 1.1
中的一种约束。
4、
@WebParam Annotation
的
partName
成员描述了
WSDL
文档中的
wsdl:part
。
5、
@WebResult Annotation
的
partName
成员描述了
wsdl:part
用来返回
WSDL
文档的值。
例如下面使用annotation
定义了一个
webservice
:
import
java.util.List;
import
javax.jws.WebMethod;
import
javax.jws.WebParam;
import
javax.jws.WebResult;
import
javax.jws.WebService;
import
com.cxf.pojo.User;
@WebService
(targetNamespace =
"http://jdk.study.hermit.org/client"
)
public
interface
UserService {
@WebMethod
(operationName=
"Insert"
)
public
void
insert(
@WebParam
(name =
"userId"
) String userid,
@WebParam
(name =
"userName"
) String username,
@WebParam
(name =
"userEmail"
) String useremail,
@WebParam
(name =
"userAge"
)
int
userage);
@WebMethod
(operationName=
"GetUserById"
)
@WebResult
(name =
"result"
)
public
User getUserById(
@WebParam
(name=
"userid"
) String userid);
@WebMethod
(operationName=
"GetAllUsers"
)
@WebResult
(name =
"result"
)
public
List getAllUsers();
}
其实现类如下所示:
import
java.util.List;
import
javax.jws.WebService;
import
com.cxf.dao.UserDao;
import
com.cxf.pojo.User;
import
com.cxf.service.UserService;
@WebService
(endpointInterface=
"com.cxf.service.UserService"
)
public
class
UserServiceImpl
implements
UserService {
private
UserDao
userDao
;
public
List getAllUsers() {
return
userDao
.findAllUser();
}
public
User
getUserById(String userid) {
return
userDao
.findUserById(userid);
}
public
void
insert(String userid, String username, String useremail,
int
userage) {
User user=
new
User();
user.setUserage(userage);
user.setUseremail(useremail);
user.setUserid(userid);
user.setUsername(username);
userDao
.insert(user);
System.
out
.println(
"insert successfully!"
);
}
public
void
setUserDao(UserDao userDao) {
this
.
userDao
= userDao;
}
}
注意:实现类中的@WebService
,其中的
endpointInterface
成员指定了该类实现的接口
在Spring
的配置文件中,需要对其进行配置:
首先在ApplicationContext.xml(Spring
的配置文件
)
中引入
CXF
的
XML Scheam
配置文件
),
如下
:
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation
=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
">
<!
—
还需要引入以下3
个关于
CXF
的资源文件,这三个文件在
cxf.jar
中
-->
<
import
resource
=
"classpath:META-INF/cxf/cxf.xml"
/>
<
import
resource
=
"classpath:META-INF/cxf/cxf-extension-soap.xml"
/>
<
import
resource
=
"classpath:META-INF/cxf/cxf-servlet.xml"
/>
…… …… …… …… …… …… …… …… …… ……
</bean>
其次就是在
Spring的配置文件中配置
webservice
,如下所示:
<
jaxws:endpoint
id
=
"userManager"
address
=
"/UserManager"
implementorClass
=
"com.cxf.service.UserService"
>
<
jaxws:implementor
>
<
bean
id
=
"userServiceImpl"
class
=
"com.cxf.service.impl.UserServiceImpl"
>
<
property
name
=
"userDao"
>
<
ref
bean
=
"userDao"
/>
</
property
>
</
bean
>
</
jaxws:implementor
>
</
jaxws:endpoint
>
注意:
①、
address
为webservice
发布的地址
②、
implementorClass
为该webservice
实现的接口
③、
<
jaxws:implementor
></
jaxws:implementor
>
之间定义的是
implementorClass
指定接口的实现类
另外,在Spring
的配置文件中配置完成后,需要修改
web.xml
文件
<
servlet
>
<
servlet-name
>
CXFServlet
</
servlet-name
>
<
servlet-class
>
org.apache.cxf.transport.servlet.CXFServlet
</
servlet-class
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
CXFServlet
</
servlet-name
>
<
url-pattern
>
/services/*
</
url-pattern
>
</
servlet-mapping
>
注意<url-pattern>/
services /*</url-pattern>配置,
CXFServlet
会拦截此类
url
,进行处理
。上面配置的webservice
将通过如下
URL
访问到:
http://localhost:8080/cxf-ws/services/
UserManager
UserManager
为
<
jaxws:endpoint
>
标签中address
属性对应的值
cxf-ws
为本项目的名称
配置完成之后,将项目部署到tomcat
上
输入URL
:
http://localhost:8080/cxf-ws/services/
UserManager
?wsdl
将会看到生成的wsdl
文件
为了能够直接访问到
com.cxf.service.UserService
中的
insert
方法,进行如下测试:
创建一个insertUser.html
页面:
<
html
>
<
head
>
<
script
type
=
"text/javascript"
>
function
post()
{
var
xmlhttp=
false
;
if
(!xmlhttp &&
typeof
XMLHttpRequest!=
'undefined'
)
{
try {
xmlhttp =
new
XMLHttpRequest();
} catch
(e)
{
xmlhttp=
false
;
}
}
if
(!xmlhttp && window.createRequest)
{
try {
xmlhttp = window.createRequest();
} catch
(e)
{
xmlhttp=
false
;
}
}
var
dest = document.getElementById(
'destination'
).value;
xmlhttp.open(
"POST"
, dest,
true
);
xmlhttp.onreadystatechange=
function
()
{
if
(xmlhttp.readyState==4)
{
document.getElementById(
"result"
).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.setRequestHeader(
"Man"
,
"POST "
+ dest +
" HTTP/1.1"
)
xmlhttp.setRequestHeader(
"MessageType"
,
"CALL"
)
xmlhttp.setRequestHeader(
"Content-Type"
,
"text/xml"
);
var
texta = document.getElementById(
'texta'
);
var
soapmess = texta.value;
xmlhttp.send(soapmess);
}
</
script
>
</
head
>
<
body
>
hi there
<
form
id
=
"forma"
>
<
textarea
id
=
"texta"
rows
=
"10"
cols
=
"80"
>
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<
I
nsert>
<userId>2005221104210066</userId>
<userName>Leon Cao</userName>
<userEmail>caohongliang2@163.com</userEmail>
<userAge>23</userAge>
</
I
nsert>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</
textarea
>
<
p
>
<
input
type
=
"text"
id
=
"destination"
size
=
"50"
value
=
"
http://localhost:8080/cxf-ws/services/UserManager
"
></
input
>
<
p
>
<
input
type
=
"button"
onclick
=
"post()"
name
=
"submit"
value
=
"Submit"
></
input
>
</
form
>
<
div
id
=
"container"
></
div
>
Result:
<
hr
>
<
div
id
=
"result"
>
</
div
>
<
hr
>
<
div
id
=
"soap"
>
</
div
>
</
body
>
</
html
>
在该测试页面需要注意标记为红色的部分,如下是一个soap message
:
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<
I
nsert>
<userId>2005221104210066</userId>
<userName>Leon Cao</userName>
<userEmail>caohongliang2@163.com</userEmail>
<userAge>23</userAge>
</
I
nsert>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
注意:
①
<SOAP-ENV:Body
>
标签之间
<
I
nsert>
对应
@WebMethod
(operationName=
"Insert"
)
中的operationName
属性的值
②
<userId>
、
<userName>
、
<userEmail>
、
<userAge>
标签对各自对应着insert
方法中
@WebParam
中name
属性的值:
@WebParam
(name =
"userId"
) String userid,
@WebParam
(name =
"userName"
) String username,
@WebParam
(name =
"userEmail"
) String useremail,
@WebParam
(name =
"userAge"
)
int
userage);
当向webservice
上发送该
soap
消息的时候将自动解析该
soap
消息,将
Insert
解析为
insert
方法,将
<userId>
、
<userName>
、
<userEmail>
、
<userAge>
标签对之间的值解析为相对应的参数
userid
、
username
、
useremail
、
userage
③
<
input
type
=
"text"
id
=
"destination"
size
=
"50"
value
=
"
http://localhost:8080/cxf-ws/services/UserManager
"
></
input
>
该文本标签中的value
属性为
webservice
的地址
运行该页面,点击Submit
后将在数据库中插入一行数据,即调用了
inser
方法成功
相关推荐
这个项目"Spring+CXF+tomcat开发webservice"旨在教你如何利用这些技术搭建一个完整的Web服务环境,包括服务端和服务端客户端的实现。 **Spring** 是一个广泛使用的Java企业级应用开发框架,它提供了依赖注入(DI)...
Web项目中基于Maven与Spring整合的WebService之cxf的实现⬇️ 详情请参考如下链接: https://locqi.github.io/locqi.com/2018/09/05/Eclipse+Maven+Spring+CXF-create-WebService/
在Java世界中,开发Web服务的一个常见选择是使用Spring框架结合Apache CXF。Spring作为一个强大的轻量级框架,提供了丰富的功能,包括依赖注入、AOP(面向切面编程)以及企业级应用的支持。而CXF则是一个优秀的开源...
Spring + cxf = webservice 完整实例源码免费下载 完全免费。此资源仅为文档提供。 版权为百度文档 "Spring + cxf = webservice 完整实例源码免费下载" 所有。
在Java开发领域,Spring框架以其强大的依赖注入和面向切面编程能力被广泛应用,而CXF则是一个优秀的开源服务开发框架,支持SOAP和RESTful服务。当Spring与CXF结合使用时,可以方便地创建和消费Web服务。本篇文章将...
在使用Spring和Apache CXF开发Web服务时,我们可以利用CXF提供的强大功能,与Spring框架进行无缝集成。CXF允许开发者通过注解和Spring配置文件来轻松地暴露和消费Web服务。以下是关于如何使用这些技术的一些关键知识...
标题中的“spring+cxf的webservice”指的是使用Spring框架与Apache CXF库共同构建Web服务的过程。Apache CXF是一个开源的Java框架,主要用于创建和消费Web服务,它支持SOAP、RESTful等多种通信协议。Spring框架则是...
【Spring+cxf=WebService】是将Spring框架与Apache CXF集成来实现Web服务的一种常见方式。Spring是一个强大的Java企业级应用开发框架,它提供了一种模块化和灵活的方式来组织和管理应用程序的组件。而CXF则是一个...
使用jaxws开发webservice。 Webservice三要素 Wsdl(webservice使用说明书)重点掌握 Soap(jaxws开发webservice的传输协议)重点掌握 UDDI(了解) Webservice的使用场景分析(掌握) 学会jaxws基本开发方法...
本文将深入探讨如何使用Spring和CXF来发布WebService服务。 首先,Spring是一个开源的Java平台,它提供了全面的编程和配置模型,用于简化企业级应用的开发。Spring框架的核心特性包括依赖注入、面向切面编程(AOP)...
Spring框架是Java企业级应用开发的首选,而CXF是一个强大的开源服务框架,支持创建和消费Web服务。本教程将深入探讨如何利用Spring和CXF来实现基于HTTP和HTTPS的Web服务,并且将涉及到HTTPS的证书配置。 1. **...
本教程将基于"Spring+CXF开发WebService源代码"的主题,深入探讨如何利用这两者创建和消费Web服务。 首先,Spring是一个开源的应用框架,提供了一个全面的编程和配置模型,适用于Java应用程序。它为开发企业级应用...
SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验 ...通过本实验,我们可以学习到Spring Boot的自动配置和依赖管理功能、Mybatis的数据库操作功能和CXF的WebService开发功能。
【标题】"mybatis+spring+cxf Webservice框架"是一个集成性的开发框架,它结合了三个主流的技术组件:MyBatis、Spring和Apache CXF,用于构建高效、灵活且易于维护的Web服务。MyBatis是一个优秀的持久层框架,Spring...
使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻...
在Java开发中,Spring是一个广泛使用的轻量级框架,用于简化企业级应用的开发,而CXF则是一个开源的服务栈,主要用于构建和部署Web服务。这两者的结合使得开发人员能够轻松地创建、消费和管理SOAP或RESTful Web服务...
ibatis+spring+cxf+mysql搭建webservice的客户端,文章地址在http://blog.csdn.net/cenyi2013/article/details/17315755. 服务端源码的下载地址在http://download.csdn.net/detail/cenyi2012/6712729
【描述】"maven+spring+cxf 的webservice开发源码,欢迎大家指点,谢谢"表明这个项目是一个开源的学习资源,包含了实际的源代码,开发者可以借此学习如何在实际项目中使用Maven、Spring和CXF来开发Web服务。...
本文将通过具体的源码示例,详细介绍如何在Spring Boot环境中使用CXF来搭建一个简单的WebService服务端。 #### 二、环境准备与依赖引入 为了确保项目的顺利进行,首先需要在`pom.xml`中添加相关的依赖: ```xml ...
Spring框架是Java企业级应用开发的首选,而CXF则是一个强大的Web服务框架,它支持SOAP和RESTful两种Web服务风格。本教程将详细介绍如何利用Spring和CXF来实现Web服务,包括服务端和客户端的开发。 首先,让我们深入...