- 浏览: 48050 次
- 性别:
- 来自: 长沙
-
最新评论
-
li002qwe:
String str = null;代表一个空对象,调用任何s ...
String等于null有缺陷 -
抛出异常的爱:
代码不全吧写全 看看
String等于null有缺陷 -
天涯之海:
zzhxlyc 写道jiyanliang 写道如果" ...
String等于null有缺陷 -
zzhxlyc:
jiyanliang 写道如果"aaa"换 ...
String等于null有缺陷 -
jiyanliang:
如果"aaa"换成是null将会是一个什么 ...
String等于null有缺陷
<!-- [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 方法成功
发表评论
-
ssh的优缺点
2010-05-09 20:06 1619struts 优点: 收集,验 ... -
Java代码优化,策略与方法
2010-05-04 13:52 931Java代码优化,策略与方法--小结 1. 如何使用Ex ... -
数据库优化
2010-04-12 22:14 1088数据库优化的目标无非是避免磁盘 I/O 瓶颈、减少 CP ... -
JAVA面试题
2010-04-10 21:20 836网上找到一些JAVA面试题,不过,不管是不是面试题,有些时候在 ... -
常见JAVA笔试题
2010-04-10 21:15 912最近遇到的一些笔试题,大部分都是宝典上面的题目 第一,谈谈f ... -
面试70技巧
2010-04-10 20:51 11801、请你自我介绍一下你 ... -
常见hibernate面试题
2010-04-10 20:38 11101.Hibernate有哪几种查询数据的方式 ... -
spring 事务
2010-04-10 20:34 811一、Propagation : 对于特定的方法或方法命名模式 ... -
spring ioc是什么
2010-04-10 20:32 1464在实际的开发中,我们 ... -
spring面试题
2010-04-10 20:31 4489一、spring工作原理: 1.spring mvc请所有的 ...
相关推荐
Spring+CXF+tomcat开发webservice,包含服务端和客户端,导入myeclipse就可以用。帮助大家搭建webservice框架,实现webservice功能。
Spring + cxf = webservice 完整实例源码免费下载 完全免费。此资源仅为文档提供。 版权为百度文档 "Spring + cxf = webservice 完整实例源码免费下载" 所有。
本java工程实现了spring+cxf快速开发webservice jar包齐全导入myeclipse可直接二次开发
Web项目中基于Maven与Spring整合的WebService之cxf的实现⬇️ 详情请参考如下链接: https://locqi.github.io/locqi.com/2018/09/05/Eclipse+Maven+Spring+CXF-create-WebService/
Spring+CXF 发布WebService服务
Spring+CXF开发WebService源代码,包括所需要的基本jar,调试通过,适合初学者入门学习。
Spring+CXF实现WebService,包含了服务端和客户端的代码。
Apache CXF 提供方便的Spring整合方法,可以通过注解、Spring标签式配置来暴露Web Services和消费Web Services 各种类型的Annotation。@WebService和@WebMethod是WSDL映射Annatotion。这些Annotation将描述Web ...
maven+spring+cxf 的webservice开发源码,欢迎大家指点,谢谢
jar包1 为Spring+cxf=webservice 提供 http://download.csdn.net/detail/sys01752/3850680 需要本jar包才能运行
mybatis+spring+cxf webservice服务 项目框架代码例子,绝对是可以参考的,信不信,只有下载之后才知道
SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验 ...通过本实验,我们可以学习到Spring Boot的自动配置和依赖管理功能、Mybatis的数据库操作功能和CXF的WebService开发功能。
WebService—CXF发布服务spring+cxf的doc文档,以及apache-cxf用到的的jar包
利用spring+cxf轻松实现webservice接口
使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻...
https使用jks,附件里有证书文件,需要自己修改路径
cxf 请求webService 方式 xml
cxf+spring+tomat开发webservice所需jar包,该jar包只需解压后导入web工程即可,亲测有用
ibatis+spring+cxf+mysql搭建webservice的客户端,文章地址在http://blog.csdn.net/cenyi2013/article/details/17315755. 服务端源码的下载地址在http://download.csdn.net/detail/cenyi2012/6712729
使用jaxws开发webservice。 Webservice三要素 Wsdl(webservice使用说明书)重点掌握 Soap(jaxws开发webservice的传输协议)重点掌握 UDDI(了解) Webservice的使用场景分析(掌握) 学会jaxws基本开发方法...