XFire 入门

今天学习了下webservice框架XFire,动手在myeclipse 6.0写了个helloworld的入门例子。

1. 新建一个webservice工程:mywebservice, 按照myeclipse的向导点next,但最后一步记得要把 XFire 1.2 HTTP Client Libraries 选中,不然调用webservice时会抛 java.lang.ClassNotFoundException: org.apache.commons.httpclient.methods.RequestEntity 这个异常,原因是缺少 commons-httpclient-3.0.jar这个jar包。

2. 编写helloworld的接口 HelloWorldService 和实现类 HelloWorldServiceImpl
接口代码:

package com.wallace;

public interface HelloWorldService {

public String sayHello();
}

实现类代码:

package com.wallace;

public class HelloWorldServiceImpl implements HelloWorldService {

public String sayHello() {
// TODO Auto-generated method stub
return "helo world";
}

}

然后配置 service.xml 如下:
其中HelloWorldService为webservice名子
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">

<service>
<name>HelloWorldService</name>
<serviceClass>com.wallace.HelloWorldService</serviceClass>
<implementationClass>
com.wallace.HelloWorldServiceImpl
</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service></beans>

也可以在包下右键新建一个webservice, 输入 webservice名子,接口,,实现类来配置service.xml ,这样的话不用手工写service.xml里的代码了.
web.xml可以不用修改,接下来在服务器上布署工程,我用的是glassfish 。
布署完,启动服务器,在firefox中输入 http://localhost:8080/mywebservice/services/HelloWorldService?wsdl
其中mywebservice是工程名, HelloWorldServcie 是 service.xml中 service name 元素所定义的内容,”wsdl”参数表示查看该 Web 服务的 WSDL文件,这样就能看到产生的wsdl文档了。

<wsdl:definitions targetNamespace="http://wallace.com">

<wsdl:types>

<xsd:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://wallace.com">

<xsd:element name="sayHello">
<xsd:complexType/>
</xsd:element>

<xsd:element name="sayHelloResponse">

<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="sayHelloResponse">
<wsdl:part name="parameters" element="tns:sayHelloResponse">
    </wsdl:part>
</wsdl:message>

<wsdl:message name="sayHelloRequest">
<wsdl:part name="parameters" element="tns:sayHello">
    </wsdl:part>
</wsdl:message>

<wsdl:portType name="HelloWorldServicePortType">

<wsdl:operation name="sayHello">
<wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest">
    </wsdl:input>
<wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse">
    </wsdl:output>
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name="HelloWorldServiceHttpBinding" type="tns:HelloWorldServicePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

<wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction=""/>

<wsdl:input name="sayHelloRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>

<wsdl:output name="sayHelloResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name="HelloWorldService">

<wsdl:port name="HelloWorldServiceHttpPort" binding="tns:HelloWorldServiceHttpBinding">
<wsdlsoap:address location="http://localhost:8080/mywebservice/services/HelloWorldService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

3。行远程调用webservice

新建一个web工程:mywebservicetest, 将mywebservicetest工程lib下的jar拷到当前工程lib下。

编写与工程lmywebservicetest同样的接口

package com.test;

public interface HelloWorldService {

public String sayHello();
}

编写测试类:

package com.test;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

public class WebServiceTest {

public static void main(String[] args) {
Service service = new ObjectServiceFactory().create(HelloWorldService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String helloUrl = "http://localhost:8080/mywebservice/services/HelloWorldService";
try {
HelloWorldService myservice = (HelloWorldService)factory.create(service, helloUrl);
System.out.println(myservice.sayHello());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

}

运行测试类,控制台得到调用webservice的结果:
helo world

你可能感兴趣的:(Web,xml,webservice,MyEclipse,Glassfish)