XFire搭建WebService和客户端访问程序

开发环境:myeclipse8.6+jdk1.6.0_29+tomcat6.0.37

JAX-WS搭建webservice:http://www.cnblogs.com/gavinYang/p/3525287.html

一、搭建WebService

1.新建一个Web Service Project



做完上述步骤后会发现项目中多了一个WebServices目录和WebServices目录下services.xml文件,web.xml会多一个Servlet配置
指明了当遇到/services/*请求时,将选用XFireConfigurableServlet来处理

2.New Web Service


做完上述步骤后会发现WebServices目录下services.xml文件多了如下内容

src目录下自动生成了两个类(我们也可以写上自己的接口方法)
UserServiceImpl.java

package com.ws.test;
//Generated by MyEclipse

public class UserServiceImpl implements IUserService {
    
    public String example(String message) {
        return message;
    }
    
}

IUserService.java

package com.ws.test;
//Generated by MyEclipse

public interface IUserService {
    
    public String example(String message);
    
}


3.部署项目到tomcat,访问url:http://localhost:8080/ws/services/UserService?wsdl

  <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions targetNamespace="http://test.ws.com" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://test.ws.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <wsdl:types>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://test.ws.com">
- <xsd:element name="example">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
- <xsd:element name="exampleResponse">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
  </xsd:schema>
  </wsdl:types>
- <wsdl:message name="exampleRequest">
  <wsdl:part name="parameters" element="tns:example" /> 
  </wsdl:message>
- <wsdl:message name="exampleResponse">
  <wsdl:part name="parameters" element="tns:exampleResponse" /> 
  </wsdl:message>
- <wsdl:portType name="UserServicePortType">
- <wsdl:operation name="example">
  <wsdl:input name="exampleRequest" message="tns:exampleRequest" /> 
  <wsdl:output name="exampleResponse" message="tns:exampleResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="UserServiceHttpBinding" type="tns:UserServicePortType">
  <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="example">
  <wsdlsoap:operation soapAction="" /> 
- <wsdl:input name="exampleRequest">
  <wsdlsoap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output name="exampleResponse">
  <wsdlsoap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="UserService">
- <wsdl:port name="UserServiceHttpPort" binding="tns:UserServiceHttpBinding">
  <wsdlsoap:address location="http://localhost:8080/ws/services/UserService" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>
View Code

 

二、调用WebServices
1.新建一个Java Project

2.添加XFire类库至构建路径

3.我们新建一个Java类来测试调用webservice

package com.wsc.test;

import java.net.URL;

import org.codehaus.xfire.client.Client;

public class TestWs {

    public static void main(String[] args) {
        try{
            Client client = new Client(new URL("http://localhost:8080/ws/services/UserService?wsdl"));
            Object[] results = client.invoke("example", new Object[] {"Gavin"});
            System.out.println((String) results[0]);
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

输出结果为:Gavin

posted @ 2014-01-18 18:11  努力挣扎的小兵  阅读(1147)  评论(0编辑  收藏  举报