如何来用cxf结合spring开发webservice接口
2015-09-21 19:05
501 查看
CXF总结
如何来用cxf结合spring开发webservice接口。by@wangkun
下载cxf
下载地址:http://cxf.apache.org/download.html我下载的版本是:apache-cxf-3.0.6.zip
解压
建两个java web project并引入cxf相关jar包
webservice服务端:webservice客户端:
webService服务端
开始,写一个service interface
package com.webservice.server.wangkun; /** * Created by wangkun on 2015/9/21. */ import javax.jws.WebService; @WebService public interface HelloWorld { String sayHi(String text); }
接着,写我们的实现类:
package com.webservice.server.wangkun; /** * Created by wangkun on 2015/9/21. */ import javax.jws.WebService; @WebService(endpointInterface = "com.webservice.server.wangkun.HelloWorld") public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { System.out.println("sayHi called"); return "Hello " + text; } }
声明server beans
CXF包含在Spring的“好XML”的支持。对于JAX-WS,建一个 bean服务器端的endpoint。在WEB-INF目录下的CXF-servlet.xml文件:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 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"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxws:endpoint id="helloWorld" implementor="com.webservice.server.wangkun.HelloWorldImpl" address="/HelloWorld"/> </beans>
如果你想弹性的引用bean可以这样写:
<bean id="hello" class="demo.spring.service.HelloWorldImpl" /> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
ID指定Spring上下文的bean的ID。
implementor是指实现类
address 是指请求路径
设置Servlet(web.xml)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/cxf-servlet.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> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webService/*</url-pattern> </servlet-mapping> </web-app>
访问
部署到tomcat访问http://localhost:8080/webService/HelloWorld?wsdl,如果打开能看到wsdl文档,说明服务端已经发布成功。
webService客户端
利用cxf的生成工具生成客户端相关类
把cxf的可执行目录配置到系统环境变量下:打开cmd,cd到wsdl文档所在的目录(该wsdl文档是服务端所产生的),执行命令
wsdl2java wsdl2java F:\webservice_project\cxfClient\src\HelloWorld.wsdl
效果,可以看到生成的客户端代码
像endpoint用于服务端一样,在客户端使用client,创建client-beans.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!--一种方式--> <bean id="helloClient" class="com.webservice.server.wangkun.HelloWorld" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.webservice.server.wangkun.HelloWorld"/> <property name="address" value="http://localhost:8080/webService/HelloWorld"/> </bean> <!--另外一种jaxws:client方式 --> <!-- <jaxws:client id="helloClient" serviceClass="com.webservice.server.wangkun.HelloWorld" address="http://localhost:8080/webService/HelloWorld" />--> </beans>
编写测试类:
package com.webservice.test; import com.webservice.server.wangkun.HelloWorld; import org.springframework.context.support.ClassPathXmlApplicationContext; public final class Client { private Client() { } public static void main(String args[]) throws Exception { // START SNIPPET: client ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"}); HelloWorld client = (HelloWorld)context.getBean("client"); String response = client.sayHi("Joe"); System.out.println("Response: " + response); System.exit(0); // END SNIPPET: client } }
相关文章推荐
- java 银联接口开发
- java虚拟机常用命令工具
- JMS集群部署问题 java.net.ConnectException: Connection refused; No available router to destination
- SpringMvc登陆拦截器的基本使用
- Java中的数据类型
- JDK的下载,安装与配置
- java各种去除空格
- Spring实现AOP的4种方式
- Java位运算
- java微云快速开发平台
- java学习笔记之泛型
- 从一开始,说出事java匿名内部类
- java.lang.IllegalArgumentException: Negative time
- [置顶] JAVA 垃圾收集器与内存分配策略
- 成功搭建CXFWebService服务端(集成spring)
- Java垃圾回收机制2
- eclipse maven plugin 插件 安装 和 配置
- Java垃圾回收机制1
- 第十二章:File类和流
- Java的String为什么要设计成final