转自:http://essay.iteye.com/blog/1775288
一、 准备工作。
1、下载apache-cxf的应用包,地址:http://cxf.apache.org/download.html,我选择的是2.4.1版本。
二、发布webservices
1.新建webproject ,并加入apache-cxf-2.4.1\lib所有包,编写要发布的web service接口和实现.这一步,与前面一样。
1)创建一个接口类,并加上webservice标记
- import javax.jws.WebService;
- @WebService
- public interface HelloWorld {
- public String sayHello(String text);
- }
2)创建上面这个接口的实现类
- import javax.jws.WebService;
- @WebService(endpointInterface="test.HelloWorld")
- public class HelloWorldImpl implements HelloWorld {
- public String sayHello(String text) {
- return "Hello" + text ;
- }
- }
@WebService 注解表示是要发布的web服务,endpointInterface的值是该服务类对应的接口。
2.在spring-cxf.xml配置发布的web service
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <beansxmlnsbeansxmlns="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" >
- <importresourceimportresource="classpath:META-INF/cxf/cxf.xml"/>
- <importresourceimportresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
- <importresourceimportresource="classpath:META-INF/cxf/cxf-servlet.xml"/>
-
- <beanidbeanid="hello"class="test.HelloWorldImpl"/>
- <jaxws:endpointidjaxws:endpointid="helloWorld"implementor="#hello"
- address="/HelloWorld"/>
- </beans >
注意:<jaxws:endpointid="helloWorld"implementor="#hello" address="/HelloWorld"/>
id:指在spring配置的bean的ID.
Implementor:指明具体的实现类.
Address:指明这个web service的相对地址,
3. 配置web.xml文件:
- <?xmlversionxmlversion="1.0"encoding="UTF-8"?>
- <web-appversionweb-appversion="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
- <context-param >
- <param-name > contextConfigLocation</param-name >
-
-
- <param-value > /WEB-INF/spring-cxf.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 >
4.部署到tomcat服务器,输入:http://localhost:8080/<web-app-name>/HelloWorld?wsdl,将显示这个web service的wsdl.
注意:如果web.xml配置<servlet-name > CXFServlet</servlet-name >
<url-pattern > /ws/*</url-pattern >
则访问地址为:http://localhost:8080/<web-app-name>/ws/HelloWorld?wsdl
到此webservices就发布成功了。
三、创建一个客户端来调用webservices
1、同样新建java project ,并加入apache-cxf-
2.0.7
\lib所有包,添加到Build Path;
2、将webservice的接口类导出成jar包,也添加到Build Path,主要目的是客户端要用到服务端的HelloWorld这个类。如果不想导入这个jar包也可以,只要在客户端创建一个一摸一样的接口类:HelloWorld,特别要注意以下两点:
1)接口前面要添加@Webservice的标记,不然会抛出一个 javax.xml.ws.WebServiceException: Could not find wsdl:binding operation info for web method sayHelloWorld.
2)包路径也要一样,不然会抛出一个ClassCastException: $Proxy29 cannot be cast to...
3、配置spring-client.xml
- <beansxmlnsbeansxmlns="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-2.0.xsd
- http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd" >
-
- <beanidbeanid="client"class="test.HelloWorld"
- factoryfactory-bean="clientFactory"factory-method="create"/>
-
- <beanidbeanid="clientFactory"class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" >
-
-
- <propertynamepropertyname="serviceClass"value="test.HelloWorld"/>
-
-
- <propertynamepropertyname="address"value="http://localhost:8080/<web-app-name>/HelloWorld"/>
- </bean >
- </beans >
4.测试:
- importorg.springframework.context.ApplicationContext;
- importorg.springframework.context.support.ClassPathXmlApplicationContext;
-
- importtest.HelloWorld;
-
- public class Test {
-
- public static void main(String[] args) {
-
- ApplicationContext ctx =newClassPathXmlApplicationContext(
- "spring-client.xml");
- HelloWorld client = (HelloWorld) ctx.getBean("client");
- String result = client.sayHello("Glen!");
- System.out.println(result);
- }
- }
结果输出“Hello,Glen!”,测试通过,至此一个webservice的调用也成功了。