基于CXF和SSH框架的WebService发布,测试
以登陆功能为例,首先编写webservice接口,代码如下:
import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import org.nova.crm.entity.TblUser; @WebService public interface UserService { @WebMethod public 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包导入:

打开web.xml,在其中加入如下servlet配置信息:
<!-- 配置CXFService框架 --> <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配置的是访问所有webservice接口的存放路径,注意如果是SSH框架下,struts的url-pattern需要改成*.action否则CXF不能截获访问webservice的请求。
下面将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
添加后效果:
<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">
在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" />
这里的address属性配置的是这个webservice接口的地址。
到这里,我们的webservice接口就已经发布出去了,可以通过浏览器来访问wsdl文件了。

可以看到url地址中,项目访问路径后的/services/userservice?wsdl,其中/services是我们在web.xml当中配置的存放所有webservice接口的路径,而/userservice?wsdl则是我们想要访问的webservice的地址(在Spring配置文件中配置的)。
浙公网安备 33010602011771号