欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

再写一次webservice的例子,以及多种形式调用

更新时间: 2014-01-05 00:52:00 责任编辑: Author_N1

 

再写一次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();
		  }




下面是需要的包

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 00:52:00
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/755/
本文WWW.DOC100.NET DOC100.NET版权所有。