使用CXF簡單開發webservice的實例

1、建立一個web Project,導入相應的jar包

點擊下載cxf的jar包

2、定義將要被暴露成webservice的接口

package com.xzb.demo.interfaces;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
	public String sayHello(String name);
}

3、定義實現接口的Impl

package com.xzb.demo.impl;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import com.xzb.demo.interfaces.HelloWorld;

@WebService
@SOAPBinding(style=Style.RPC)
public class HelloWorldImpl implements HelloWorld {

	public String sayHello(@WebParam(name="name")String name) {
		return name+" say:Hello world!";
	}
}

4、發佈及訪問web Service

(1)代碼實現

發佈web Service:

package com.xzb.demo.serviceApp;

import javax.xml.ws.Endpoint;
import com.xzb.demo.impl.HelloWorldImpl;

public class WebServiceApp {
	public static void main(String[] args) {
		 System.out.println("web service start");
         HelloWorldImpl implementor= new HelloWorldImpl();
         String address="http://localhost:80/helloWorld";
         Endpoint.publish(address, implementor);
         System.out.println("web service started");
	}

}

運行以上程序後,訪問地址http://localhost/helloWorld?wsdl,即可查看到對應的wsdl。

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:ns1="http://interfaces.demo.xzb.com/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.demo.xzb.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorldImplService" targetNamespace="http://impl.demo.xzb.com/">
<wsdl:import location="http://localhost/helloWorld?wsdl=HelloWorld.wsdl" namespace="http://interfaces.demo.xzb.com/"></wsdl:import>
<wsdl:binding name="HelloWorldImplServiceSoapBinding" type="ns1:HelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldImplService">
<wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldPort">
<soap:address location="http://localhost:80/helloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
備註:雖然在Impl中採用註釋的方式標識接口方法所需參數@WebParam(name="name"),但不起作用。試了之後,發現如果接口不採用@WebService,只在接口實現類上採用@WebService,@WebParam(name="name"),在wsdl上即可看到所需參數和返回值等信息。


客戶端訪問web Service:

package com.xzb.demo.serviceClient;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.xzb.demo.interfaces.HelloWorld;

public class HelloWorlClient {
	public static void main(String[] args) throws Exception {
		
	     JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
         svr.setServiceClass(HelloWorld.class);
         svr.setAddress("http://localhost:80/helloWorld");
         HelloWorld hw = (HelloWorld) svr.create();
         System.out.println(hw.sayHello("xzb"));
		
	}

}

client運行結果:

2013-10-18 10:38:49 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://interfaces.demo.xzb.com/}HelloWorldService from class com.xzb.demo.interfaces.HelloWorld
xzb say:Hello world!

(2)與spring結合實現

在spring配置文件上發佈webservice和客戶端訪問webservice的配置。配置代碼如下:

<?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"/>
 
                 <!--在項目的  /helloWorld   路徑將 com.xzb.demo.impl.HelloWorldImpl 暴露出去-->
                 <jaxws:endpoint 
                              id="helloWorld"
                              implementor="com.xzb.demo.impl.HelloWorldImpl"
                              address="/helloWorld"  />
   
                <!-- 客戶端訪問webservice配置 -->
                <bean id="client" class="com.xzb.demo.interfaces.HelloWorld" 
                           factory-bean="clientFactory" factory-method="create"/>
    
                 <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
                            <property name="serviceClass" value="com.xzb.demo.interfaces.HelloWorld"/>
                            <property name="address" value="http://localhost:80/CXFWebServiceTest/webservice/helloWorld"/>
                 </bean>
                
                 
     </beans>

然後,訪問http://localhost/CXFWebServiceTest/webservice/helloWorld?wsdl可以查看wsdl,client只要通過訪問http://localhost:80/CXFWebServiceTest/webservice/helloWorld,就可以調用到接口com.xzb.demo.interfaces.HelloWorld提供的方法。

package com.xzb.demo.serviceClient;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xzb.demo.interfaces.HelloWorld;

public class HelloWorlClient {
	public static void main(String[] args) throws Exception {
		
		  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		  HelloWorld client = (HelloWorld)context.getBean("client");
		  System.out.println(client.sayHello("xzb"));
		
	}

}





初步接觸cxf,尚有很多不清楚的地方,如果錯誤,請指正。(以上client訪問webservice,必須依賴於service端的接口,才能調用到正確的方法,但感覺從wsdl上得出信息貌似不太容易,因爲目前還不太熟悉。)






發佈了20 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章