基于CXF和SSH框架的WebService发布,测试(以http://www.cnblogs.com/devilcx/archive/2012/05/03/2481132.html)为基础
2017-10-08 23:34
597 查看
import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;import org.nova.crm.entity.TblUser;@WebServicepublic interface UserService {@WebMethodpublic TblUser loginUser(@WebParam(name="username")String username, @WebParam(name="password")String password);}这个接口一定要有实现类来实现,实现类代码如下:
import javax.annotation.Resource; import org.nova.crm.biz.UserBiz; import org.nova.crm.entity.TblUser; import org.nova.crm.services.UserService; import org.springframework.stereotype.Component; @Component("UserService") public class UserServiceImpl implements UserService { UserBiz userBiz; @Resource(name="UserBiz") public void setUserBiz(UserBiz userBiz) { this.userBiz = userBiz; } @Override public TblUser loginUser(String username, String password) { return userBiz.loginUser(username, password); } }这里以注解的方式获取到biz层的接口,通过该接口来调用dao层的方法,要为该biz层接口添加set方法,以便注入到Spring中,接下来配置CXF框架将所需jar包导入:若使用maven配置则添加如下依赖:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.0.1</version> </dependency>打开web.xml,在其中加入如下servlet配置信息:
<!-- 配置CXFService框架 --><!-- 1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。 2)它的值必须是一个整数,表示servlet应该被载入的顺序 2)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet; 3)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。 4)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。 5)当值相同时,容器就会自己选择顺序来加载。 所以,<load-on-startup>x</load-on-startup>,中x的取值1,2,3,4,5代表的是优先级,而非启动延迟时间。 --><servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>30</load-on-startup></servlet><servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/cxf/*</url-pattern></servlet-mapping>这里url-pattern配置的是访问所有webservice接口的存放路径,注意如果是SSH框架下,struts的url-pattern需要改成*.action否则CXF不能截获访问webservice的请求。若不修改struts访问方式,则可通过如下方式放行(struts.xml文件中)
<!-- 配置代码 namespace="/cxf"需同CXFServlet的<url-pattern>/cxf/*</url-pattern>保持一致--><package name="cxf" extends="struts-default" namespace="/cxf">
<action name="*">
<result>{1}</result>
</action>
</package>
下面将CXF用Spring管理起来,编写Spring配置文件,在beans标签中加入如下属性:
xmlns:jaxws=http://cxf.apache.org/jaxws在beans标签的xsi:schemaLocation属性中添加:
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd[/code] 添加后效果:<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">[/code] 在beans标签中导入CXF的配置文件<!-- 导入cxf框架的配置文件 旧版本中需要,如导致控制台报错可删除 --><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" />最后一步,将我们上面写的webservice接口注入进来:<jaxws:endpoint id="userService" implementor="org.nova.crm.services.impl.UserServiceImpl" address="/userservice" />还可使用 jaxws:server方式发布:配置如下<jaxws:server id="cxfService" address="/customerServiceCxf"><jaxws:serviceBean><!--customerService 为spring配置中service实现类的name--><ref bean="customerService"/></jaxws:serviceBean></jaxws:server>这里的address属性配置的是这个webservice接口的地址。到这里,我们的webservice接口就已经发布出去了,可以通过浏览器来访问wsdl文件了。可以看到url地址中,项目访问路径后的/txf/userservice?wsdl,其中/services是我们在web.xml当中配置的存放所有webservice接口的路径,而/userservice?wsdl则是我们想要访问的webservice的地址(在Spring配置文件中配置的)。
相关文章推荐
- VS2010 测试 -普通单元测试 http://www.cnblogs.com/rhythmK/archive/2012/04/20/2458832.html
- http://www.cnblogs.com/Matrix54/archive/2012/05/03/2481260.html
- http://www.cnblogs.com/Matrix54/archive/2012/05/03/2481260.html
- 开源性能测试工具 - Apache ab 介绍 ---http://www.cnblogs.com/jackei/archive/2006/07/18/454144.html
- CodeSmith基础(一) http://www.cnblogs.com/Bear-Study-Hard/archive/2005/12/19/300112.html
- WebService开发(二) 如何使用Soap扩展 http://www.cnblogs.com/wayne-ivan/archive/2007/06/19/788541.html
- 单向关系中的JoinColumn(转自:http://www.cnblogs.com/cxccbv/archive/2009/01/22/1380010.html)
- http://www.cnblogs.com/Matrix54/archive/2012/05/03/2481260.html
- 群发UDP的性能测试 (摘自:http://www.cnblogs.com/trywebservice/archive/2008/02/19/1073642.html)
- 使用Sqlserver事务发布实现数据同步(引:http://www.cnblogs.com/daizhj/archive/2009/11/18/1605293.html )
- XML WebService完全实例详细解析【转:http://www.cnblogs.com/xqzhao/archive/2008/01/18/1044574.html】
- Asp.net 基础 ( 一 ) 转自http://www.cnblogs.com/cyzyimao/archive/2010/07/20/1781495.html
- 发布一套JAVASCRIPT控制的WEBGIS源码http://www.cnblogs.com/huzhiming-2006/archive/2007/04/25/727325.html
- 爬虫基础学习 转【http://www.cnblogs.com/huangxincheng/archive/2012/11/08/2759752.html】
- http://www.cnblogs.com/yycxbjl/archive/2010/04/20/1716689.html
- http://www.cnblogs.com/devil-91/archive/2012/05/10/2493611.html
- Asp.net 基础(二) 转自http://www.cnblogs.com/cyzyimao/archive/2010/07/20/1781497.html
- 对WebService的一些封装技巧总结(http://www.cnblogs.com/sxwgf/archive/2011/07/10/something-about-webservice.html
- WebService开发(一) 如何使用Soap头 http://www.cnblogs.com/wayne-ivan/archive/2007/06/19/788539.html
- 发布一款层次下拉列表控件 转自 http://www.cnblogs.com/studyzy/archive/2008/02/25/1080289.html?