WebService的实现——cxf
2013-10-10 23:30
309 查看
一、所需的jar包(http://download.csdn.net/detail/huangzebiao007/6380241)
二、实例
1、创建对外发布的接口和实现类
2、创建服务发布类
3、创建客户端
先启动发布类,然后在启动客户端,输出:hzb:hello world!
三、Spring集成cxf
1、创建一个WebServiceCXF项目用于发布webservice,引入上面的jar包
2、创建一个WebServiceClient客户端项目用于访问webservice,引入上面的jar包和引入上面的整个项目
注意:上面是假设webservice发布是能拿到整个项目,所以客户端访问才能直接导入该包,如果不是的话,那就要通过访问wsdl文件,然后根据该文件得到接口。
二、实例
1、创建对外发布的接口和实现类
package com.hzb; import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHello(String name); }
package com.hzb; import javax.jws.WebService; public class HelloWorldImpl implements HelloWorld { public String sayHello(String name) { return name+":hello world!"; } }
2、创建服务发布类
package com.hzb; import javax.xml.ws.Endpoint; public class WebServiceApp { public static void main(String[] args) { System.out.println("web service starting"); HelloWorldImpl implementor= new HelloWorldImpl(); String address="http://localhost:8080/helloWorld"; Endpoint.publish(address, implementor); System.out.println("web service started"); } }
3、创建客户端
package com.hzb; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class HelloWorldClient { public static void main(String[] args) { JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean(); svr.setServiceClass(HelloWorld.class); svr.setAddress("http://localhost:8080/helloWorld"); HelloWorld hw = (HelloWorld) svr.create(); System.out.println(hw.sayHello("hzb")); } }
先启动发布类,然后在启动客户端,输出:hzb:hello world!
三、Spring集成cxf
1、创建一个WebServiceCXF项目用于发布webservice,引入上面的jar包
(1)创建接口 package com.cxf; import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHello(String name); } (2)创建接口实现类 package com.cxf; import javax.jws.WebService; public class HelloWorldImpl implements HelloWorld { public String sayHello(String name) { return name+":hello world!"; } } (3)配置applicationContext.xml [code]<?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" 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-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- implementorClass指向该接口的实现类, address指定该接口暴露的地址(前面是http://ip:port/project/web.xml中配置的访问前缀) --> <jaxws:endpoint id="helloWorld" implementorClass="com.cxf.HelloWorldImpl" address="/HelloWorld" /> </beans> (4)在web.xml文件中配置cxf和spring <?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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/applicationContext.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>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> </web-app> 项目部署到web服务器后,就可以通过http://localhost:8080/WebServiceCXF/webservice访问到发布的所有接口了
2、创建一个WebServiceClient客户端项目用于访问webservice,引入上面的jar包和引入上面的整个项目
(1)配置applicationContext.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" 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"> <!-- serviceClass是所要访问的webService接口,而address是要访问的接口地址 --> <jaxws:client id="helloClient" serviceClass="com.cxf.HelloWorld" address="http://localhost:8080/WebServiceCXF/webservice/HelloWorld" /> </beans> (2)创建client访问类 package com.hzb; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cxf.HelloWorld; public class Client { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); HelloWorld client = (HelloWorld) context.getBean("helloClient"); String message = client.sayHello("hzb"); System.out.println(message); } } 运行该类,得到结果:hzb:hello world!
注意:上面是假设webservice发布是能拿到整个项目,所以客户端访问才能直接导入该包,如果不是的话,那就要通过访问wsdl文件,然后根据该文件得到接口。
相关文章推荐
- Spring框架整合CXF WebService实现原理
- webService 使用CXF 实现简单的helloworld
- CXF实现WebService
- webservice之CXF注解实现(一)
- CXF 实现 WebService (1)
- webservice之CXF注解实现(二)
- CXF整合spring实现webservice
- cxf整合spring实现webservice
- WebService-CXF和spring整合实现webservice案例(Spring配置注解环境)
- java cxf实现webservice接口方式之不依赖spring
- CXF框架实现WebService开发
- 浅谈spingmvc 整合CXF +ws-security 实现webservice安全验证
- Webservice实例-使用CXF实现
- 【随记】cxf的webservice接口实现
- Spring工程集成CXF实现WebService接口
- cxf实现webservice
- 使用CXF与Spring集成实现RESTFul WebService
- cxf方式实现WebService的简单实例
- CXF实现webService框架技术