webservice的几种开发方式总结

1 将制定的webservice通过wsdl2java命令生成java类,利用该java类调用服务方法(RPC调用)。
 HiServiceImplStub stub = new HiServiceImplStub();//HiServiceImplStub为wsdl2java命令执行后的生成
 HiServiceImplStub.SayHi say= new HiServiceImplStub.SayHi();
 say.setName("chenyb");
 System.out.println(stub.sayHi(say).get_return());
 HiServiceImplStub.GetListResponse list = new HiServiceImplStub.GetListResponse();
 list.set_return(null);
 //list.set_return(param);
 System.out.println(stub.getList().get_return());
 ------------------------------------------------------
  // 使用RPC方式调用WebService
  RPCServiceClient serviceClient = new RPCServiceClient();
  Options options = serviceClient.getoptions();
  // 指定调用WebService的URL
  EndpointReference targetEPR = new EndpointReference( "http://localhost:8080/axis2/services/HiServiceImpl");
  options.setTo(targetEPR); // 指定getList方法的参数值
  Object[] opAddEntryArgs = new Object[] {"超人"};
  // 指定getList方法返回值的数据类型的Class对象
  Class[] classes = new Class[] {String.class};
  // 指定要调用的getList方法及WSDL文件的命名空间
  QName opAddEntry = new QName("http://ws.apache.org/axis2","getList");
  // 调用getList方法输出方法的返回值
  Object[] list =(Object[]) serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes);
  System.out.println(list[0]);
 2  使用xfire框架来开发webservice,客户端调用代码如下
 1)xfire.xml
<beans>
  <service xmlns="http://xfire.codehaus.org/config/1.0">
    <name>SayHiService</name>
    <namespace>http://c2n.com.pansky/SayHiService</namespace>
    <serviceClass>com.cyb.model.SayHiService</serviceClass>
    <implementationClass>com.cyb.model.SayHiServiceImpl</implementationClass>
  </service>
</beans>
2)web.xml
<servlet>
    <servlet-name>XFireServlet</servlet-name>
    <display-name>XFire Servlet</display-name>
    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/servlet/XFireServlet/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
3)程序调用服务接口
      String serviceURL = "http://localhost:8080/stove/services/SayHiService";//"http://localhost:8080/stove/services/SayHiService";
     Service serviceModel =  new ObjectServiceFactory().create(SayHiService.class,null,"http://localhost:8080/stove/services/SayHiService?WSDL",null);
     XFireProxyFactory serviceFactory = new XFireProxyFactory();

     try{
       SayHiService service = (SayHiService) serviceFactory.create(serviceModel,serviceURL);
       Client client = Client.getInstance(service);
       //client.addOutHandler(new OutHeaderHandler());
      // client.setIntParameter("http.socket.timeout",3000);
       // disable timeout
       //client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT,"1");
       String hello = service.sayHi("张山疯");
       System.out.println("服务器对[张山疯] 的回答是:" + hello );
       hello = service.sayHi(null);
       System.out.println("服务器胡言乱语说:" + hello );
 
     } catch (MalformedURLException e) {
       e.printstacktrace();
     }
   }


3 使用cxf框架开发webservice,客户端调用程序方式:
1)程序调用服务方法
 ClasspathXmlApplicationContext context  = new ClasspathXmlApplicationContext(new String[] {"classpath:client/client-bean.xml"});
 ClasspathXmlApplicationContext context0  = new ClasspathXmlApplicationContext(new String[] {"classpath:client/beans.xml"});

 //从client-bean.xml中获取bean
 AccountDeposit client = (AccountDeposit)context.getBean("client");
 double response = client.avg("940200002595585","20080101","20080331");
 System.out.println("first service return: " + response);
 //直接从beans.xml中获取
 SecondService client_0 = (SecondService)context0.getBean("userService");
      List<String> list = client_0.getList();
 System.out.println(client_0.getMyName()+"----second service return:"+list.get(1));
 System.exit(0);
 
2)beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxwshttp://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" />
<jaxws:endpoint id="accountDeposit" implementor="com.cyb.model.AccountDepositImpl" address="/AccountDeposit" />
<jaxws:endpoint id="sencondService"  address="/SencondService" ><!-- 服务名及访问目录 -->
 <jaxws:implementor ref="userService" /><!-- 实现类 -->
</jaxws:endpoint>
<bean id="userService" class="com.cyb.model.SecondServiceImpl"/>
</beans>
<!-- END SNIPPET: beans -->


3)client-bean.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://cxf.apache.org/jaxwshttp://cxf.apache.org/schema/jaxws.xsd">
  <!-- 定义一个客户端bean -->
  <bean id="client" class="com.cyb.model.AccountDeposit" factory-bean="clientFactory" factory-method="create"/>
            <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyfactorybean">
  <property name="serviceClass" value="com.cyb.model.AccountDeposit"/>
  <property name="address" value="http://localhost:8080/Cxftest/AccountDeposit"/>
  </bean>
  
  <!--可以不用,直接在beans.xml里边取service -->
  <bean id="client0" class="com.cyb.model.SecondService" factory-bean="clientFactory" factory-method="create"/>
  <bean id="clientFactory0" class="org.apache.cxf.jaxws.JaxWsProxyfactorybean">
  <property name="serviceClass" value="com.cyb.model.SecondService"/>
  <property name="address" value="http://localhost:8080/Cxftest/SecondService"/>
  </bean>
</beans>  

4)
  <servlet>
  <servlet-name>CXFServlet</servlet-name>
  <!--  <display-name>CXF Servlet</display-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>/services/*</url-pattern><!-- 服务访问目录 -->
 </servlet-mapping>
5)接口类定义
@WebService
public interface SecondService {
  List<String> getList();
  String  getMyName();
}


6)实现类定义
@WebService(endpointInterface = "com.cyb.model.SecondService")
public class SecondServiceImpl implements SecondService{

 public List<String> getList() {
  List<String> list = new ArrayList<String>();
  list.add("str1");
  list.add("str2");
  return list;
 }

 public String getMyName() {
  return "chenyb";
 }

}

 http://www.blogjava.net/zjhiphop/archive/2009/04/29/webservice.html 

相关文章

1.使用ajax调用varxhr;functioninvoke(){if(window.ActiveXO...
               好不容易把WebService服务器...
1新建一个工程项目用来做服务端增加一个MyService1类文件pac...
packagecom.transsion.util;importjava.io.BufferedReader;i...
再生产wsdl文件时重写描述文件1usingSystem;2usingSystem.Co...
一般情况下,使用eclipse自带的jax-ws生成webservice会自动生...