Spring+WebService配置
1、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>/ws/*</url-pattern> </servlet-mapping>
2、applicationContext.xml配置片段
<?xmlversion="1.0" encoding="UTF-8"?> <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:tx="http://www.springframework.org/schema/tx"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"> <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 id="wsTestService"class="com.abel.test.ws.impl.WsTestServiceImpl" /> <jaxws:endpoint id="wsTestServiceWs"implementor="#wsTestService" address="/wsTestService"> </jaxws:endpoint> ……..
3、编写webservice接口,记住加上@WebService标注
packagecn.com.abel.test.ws; importjavax.jws.WebService; @WebService public interface WsTestService{ public String helloWord(String str); }
4、实现上面的webservice接口
packagecn.com.abel.test.ws.impl; import javax.jws.WebService; importorg.springframework.stereotype.Service; importcn.com.abel.test.ws.WsTestService; @Service("wsTestService") //声明web服务,并指定接口路径 @WebService(endpointInterface= "com.abel.test.ws.WsTestService") public class WsTestServiceImpl implements WsTestService{ public String hello(String str){ return "hello" + str; } }
5、启动应用,访问http://localhost:8080/ws/wsTestService?wsdl
如若能正常访问则至此服务端配置完成,下面开始配置客户端
6、把上面服务端的WsTestService接口拷贝过来,包名和类名必须都一致不能修改
7、客户端的applicationContext.xml
<?xmlversion="1.0" encoding="UTF-8"?> <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:tx="http://www.springframework.org/schema/tx"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <importresource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:clientid="wsTestService" serviceClass="cn.com.abel.test.ws.WsTestService" address="http://localhost:8080/ws/wsTestService" />
6、客户端调用时直接在加上@Resource(name="wsTestService")即可,wsTestService就是上面jaxws:client中的id,如:
@Controller public class IndexController{ @Resource(name="wsTestService") private WsTestService wsService; @RequestMapping("") public String index(ModelMap model){ String ss = wsService.helloWord("webservice,this's my first webservice test"); model.addAttribute("hello", "hellospring mvc, " + ss); return "index"; } }