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
XFire搭建WebService和客户端访问程序
XFire搭建WebService和客户端访问程序
XFire搭建WebService和客户端访问程序
做完上述步骤后会发现项目中多了一个WebServices目录和WebServices目录下services.xml文件,web.xml会多一个Servlet配置
指明了当遇到/services/*请求时,将选用XFireConfigurableServlet来处理
XFire搭建WebService和客户端访问程序

2.New Web Service
XFire搭建WebService和客户端访问程序
XFire搭建WebService和客户端访问程序
做完上述步骤后会发现WebServices目录下services.xml文件多了如下内容
XFire搭建WebService和客户端访问程序
src目录下自动生成了两个类(我们也可以写上自己的接口方法)
UserServiceImpl.java

XFire搭建WebService和客户端访问程序
package com.ws.test;
//Generated by MyEclipse

public class UserServiceImpl implements IUserService {
    
    public String example(String message) {
        return message;
    }
    
}
XFire搭建WebService和客户端访问程序

IUserService.java

XFire搭建WebService和客户端访问程序
package com.ws.test;
//Generated by MyEclipse

public interface IUserService {
    
    public String example(String message);
    
}
XFire搭建WebService和客户端访问程序


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

XFire搭建WebService和客户端访问程序
  <?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类库至构建路径
XFire搭建WebService和客户端访问程序

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

XFire搭建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();
        }
    }
}
XFire搭建WebService和客户端访问程序

输出结果为:Gavin

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

上一篇:Java将两位年份转换为全年


下一篇:.net core读取appsettings.json的配置