初级篇---实现篇---WebService---CXF---Spring V0.0.1
? ? ? ? 前言:任何一种技术应用与理解是受特定环境与个人知识积累所影响的,鉴于此,我会不定期修正文章,并标注出修正的内容与原因,同时在实现篇中,仅讲解如何实现,而实现的原理将会在相应的原理篇中阐述。 ? ? ? ?? ? ? ? ? WebService,是一种规范,一种提供了多系统之间信息交互的一种实现方式,据我所知可以在B/S、C/S、Android等均有涉及,下面介绍通过CXF与Spring相结合的方式的一个简单实现。 ? ? ? ? 参考资料:apache cxf 官方 user guide:http://cxf.apache.org/docs/writing-a-service-with-spring.html ? ? ? ?? ? ? ? 实现思路:某系统提供了一个对外的接口,隐藏了内部实现,其他的系统可以通过这个接口来调用相应的方法。 ? ? ? 举一个简单的例子,一个IT公司具有,考勤系统、内部论坛、绩效管理等,这些系统都需要实现Login(登录)的功能,那么如果每个系统实现都有一个自己的登录实现,将会很臃肿,那么将以什么方式解决? ? ? ? 这就是一个简单的Demo。下面将介绍具体的实现。?? ? ? ? 代码设计分为Server端与Client端, ? ? ? 对于Server端,有如下步骤: ? ? ? Step1.设计接口 ? ? ? Step2.实现结构 ? ? ? Step3.配置CXF文件 ? ? ? Step4.发布 ? ? ? 完整工程结构:Referenced Libraries中的包为apache cxf中lib下的jar包,需导入。 ? ? ? ? ? ?Step1:接口设计IHelloWorld.java
package com.hzy.demo; import javax.jws.WebService; @WebService public interface IHelloWorld { public String sayHi ( String text ); }? ? ? Step2:接口实现HelloWorldImpl.java
package com.hzy.demo; import javax.jws.WebService; @WebService ( endpointInterface="com.hzy.demo.IHelloWorld",serviceName="helloService" ) public class HelloWorldImpl implements IHelloWorld { public String sayHi(String text) { return "Server Echo: Hello Client " + text; } }? ?? ? ? ? ?Step3:配置CXF文件 ? ? ? CXF文件:application-server.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:util="http://www.springframework.org/schema/util" 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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd " > <jaxws:endpoint id="helloService" implementor="com.hzy.demo.HelloWorldImpl" address="/helloService"> </jaxws:endpoint> </beans>? ? ? ?? ? ? ? ??Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-server.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> ? ? ? ? 至此服务端搭建完成,运行验证http://localhost:8080/ws_cxf_spring_server_demo/helloService?wsld ? ? ? ? 对于Client端,可以是一个单纯的Java工程,但同样需导入apache cxf lib目录下的jar包 ? ? ? ? 完整工程结构如下: ? ? ? ?? ? ? ? ?? ? ? ? ? 对于Client端的建立,有如下步骤: ? ? ? ? Step1.接口的描述,需注意与服务端包名、接口名保持一致 ? ? ? ? Step2.配置Spring文件 ? ? ? ? Step3.访问 ? ? ? ? 实现如下: ? ? ? ? Step1. 创建接口文件? ? ??
package com.hzy.demo; import javax.jws.WebMethod; public interface IHelloWorld { @WebMethod public String sayHi ( String text ); }? ? ? ? Step2. 配置Spring文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:util="http://www.springframework.org/schema/util" 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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd " > <bean id="client" class="com.hzy.demo.IHelloWorld" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.hzy.demo.IHelloWorld" /> <property name="address" value="http://localhost:8080/ws_cxf_spring_server_demo/helloService" /> </bean> </beans>? ? ? ? Step3. 访问(Demo中,编写CXF_SPRING_CLIENT.java) package com.hzy.demo.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hzy.demo.IHelloWorld; public class CXF_SPRING_CLIENT { public static void main ( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext ( "application-spring-client.xml" ); IHelloWorld helloService = (IHelloWorld) context.getBean ( "client" ); String response = helloService.sayHi ( "I'm cxf_spring_client!" ); System.out.println ( "RE: " + response ); } }? ? ? ? 至此,Client端编写完成,与上述Server配合使用,Demo运行结果如下: ? ? ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |