分類  >  Web前端 >

再寫一次WEBservice的例子,以及多種形式調用

tags:    時間:2013-12-09 23:15:15
再寫一次WEBservice的例子,以及多種方式調用
部署webservice的服務端
web.xml

 <!-- webservice服務端 -->     <servlet>         <servlet-name>cxf</servlet-name>         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>         <load-on-startup>1</load-on-startup>     </servlet>   <servlet-mapping>         <servlet-name>cxf</servlet-name>         <url-pattern>/webservice/*</url-pattern>     </servlet-mapping> 



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" 	xmlns:context="http://www.springframework.org/schema/context" 	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/context 			    	   http://www.springframework.org/schema/context/spring-context.xsd"> 			    	 			    	 			    	 	<import resource="classpath:META-INF/cxf/cxf.xml" /> 	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  	  <!-- 默認掃描的包路徑 -->       <context:component-scan base-package="com.ishopping.webservice.*" />    	<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> 	<jaxws:endpoint id="wsMyorder" implementor="#myorder" address="/Myorder" />  </beans> 

伺服器介面
 package com.ishopping.webservice.service;  import javax.jws.WebMethod; import javax.jws.WebResult; import javax.jws.WebService;   @WebService public interface HelloWorld { 	public String say(String name); 	public String getText(); }  



服務實現類

  package com.ishopping.webservice.impl;  import javax.jws.WebService; import org.springframework.stereotype.Component; import com.ishopping.webservice.service.HelloWorld;     @Component("hello")//這個和cxf-servlet.xml中implementor="#hello" 關聯 @WebService(endpointInterface = "com.ishopping.webservice.service.HelloWorld")//介面路徑 public class HelloWorldImpl implements HelloWorld{ 	public TestDao testDao;  	public String say(String name) { 		 		 		return name; 	} 	 	@Override 	public String getText() { 		System.out.println("fffff"); 		return "成功"; 	} 	 	 } 


啟動伺服器,如果不出意外的話將會看到這個



輸入路徑




然後客戶端調用
第一種通過spring來調用

  //讀取配置文件   ApplicationContext ctx =    new ClassPathXmlApplicationContext("ClientBeans.xml");   //get到介面類型並調用   HelloWorld hello = (HelloWorld)ctx.getBean("helloClient");   String str = hello.getText();   System.err.println(str);  


 <?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:jaxrs="http://cxf.apache.org/jaxrs"        xmlns:cxf="http://cxf.apache.org/core"        xsi:schemaLocation="http://www.springframework.org/schema/beans                http://www.springframework.org/schema/beans/spring-beans.xsd             http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd             http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd             http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">  <jaxws:client id="helloClient"     <!--服務地址-->        address="http://www.goudzd.com:8088/goudzd/webservice/Helloworld"      <!--客服端調用類-->        serviceClass="com.ishopping.webservice.service.HelloWorld">  </jaxws:client> </beans>  


注意的是HelooWorld介面必須和伺服器路徑一致

第二種:通過CXF調用

commons-logging-1.1.1.jar
cxf-2.3.3.jar
servlet-api.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.7.jar

需要用到的包

 JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean(); soapFactoryBean.setAddress("http://www.goudzd.com:8088/goudzd/webservice/HelloWorld?wsdl"); 		soapFactoryBean.setServiceClass(HelloWorld.class); 		HelloWorld order =(HelloWorld) soapFactoryBean.create(); 		String str = order.say("dddddddd");  


注意的是HelooWorld介面必須和伺服器路徑一致

第三種 通過soap鏈接
需要用到的包

ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar




注意紅框的地方




 //裡面地址對應上面圖片的namespace  SoapObject request = new SoapObject("http://service.webservice.ishopping.com/", "getText"); //這個是配置參數 //		  request.addProperty("Fahrenheit",txtFar.getText().toString());  		  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 		  envelope.setOutputSoapObject(request); 		  envelope.dotNet = true; 		  try { //對應圖片上的soap:address                HttpTransportSE androidHttpTransport = new HttpTransportSE("http://10.0.98.19:8088/goudzd/webservice/HelloWorld"); //call的第一個參數對應圖片上的soapAction=""               androidHttpTransport.call("", envelope);               SoapObject result = (SoapObject)envelope.bodyIn; //這裡我獲取第一個數據               System.out.println(result.getProperty(0).toString()); 		  } catch (Exception e) {               e.printStackTrace(); 		  }  



下面是需要的包

推薦閱讀文章

Bookmark the permalink ,來源:互聯網