初级篇---实现篇---WebService---CXF---Spring V0.0.1
2013-08-11 01:12
393 查看
前言:任何一种技术应用与理解是受特定环境与个人知识积累所影响的,鉴于此,我会不定期修正文章,并标注出修正的内容与原因,同时在实现篇中,仅讲解如何实现,而实现的原理将会在相应的原理篇中阐述。
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(登录)的功能,那么如果每个系统实现都有一个自己的登录实现,将会很臃肿,那么将以什么方式解决?
思路如下,设计一个UC(User Center,用户中心),由UC实现用户的登录,对外提供Login这个服务,所有的其他系统都通过这个UC实现登录。
这就是一个简单的Demo。下面将介绍具体的实现。
代码设计分为Server端与Client端,
对于Server端,有如下步骤:
Step1.设计接口
Step2.实现结构
Step3.配置CXF文件
Step4.发布
完整工程结构:Referenced Libraries中的包为apache cxf中lib下的jar包,需导入。
Step1:接口设计IHelloWorld.java
Step3:配置CXF文件
CXF文件:application-server.xml
Web.xml
至此服务端搭建完成,运行验证http://localhost:8080/ws_cxf_spring_server_demo/helloService?wsld
对于Client端,可以是一个单纯的Java工程,但同样需导入apache cxf lib目录下的jar包
完整工程结构如下:
对于Client端的建立,有如下步骤:
Step1.接口的描述,需注意与服务端包名、接口名保持一致
Step2.配置Spring文件
Step3.访问
实现如下:
Step1. 创建接口文件
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(登录)的功能,那么如果每个系统实现都有一个自己的登录实现,将会很臃肿,那么将以什么方式解决?
思路如下,设计一个UC(User Center,用户中心),由UC实现用户的登录,对外提供Login这个服务,所有的其他系统都通过这个UC实现登录。
这就是一个简单的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运行结果如下:
相关文章推荐
- cxf实现webservice服务端
- spring boot-实现WebService(CXF实现)的拦截器(Interceptor)
- WebService系列博客{十}[CXF简单案例实现]
- cxf 实现webservice 案例(转)
- spring如何集成cxf实现webservice接口功能详解
- CXF+Spring实现webservice
- cxf-Springmvc-maven实现webservice教程
- webservice 完整 实现方式 服务端 客户端 (cxf)
- CXF实现简单webservice应用、集成spring发布到tomcat
- CXF和spring整合实现webservice实例
- CXF实现和Spring整合发布WebService
- CXF实现webService服务(一)
- cxf + spring 实现的webservice的 异常
- 使用CXF实现Webservice的服务接口提供以及相关的客户端实现
- 复习webservice (cxf实现)
- 采用spring,maven,xfire/cxf 实现SOAP的WebService,并用C#作为客户端调用
- cxf整合spring实现webservice
- CXF实现webservice
- cxf-Springmvc-maven实现webservice
- cxf实现webservice简单插入,查询,和返回结构集