XFire WebService开发快速起步
环境:
XFire-1.2.6
JDK1.5
MyEclipse 6.5
Tomcat-5.5.27
Windows XP Professional简体中文版
软件下载地址:
以下是开发步骤:
1、创建工程
打开MyEclipse 6.5,新建一个WebService工程。如下图
然后一路next,直到完成。
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
< web-app xmlns ="http://java.sun.com/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.5" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]" >
< servlet >
< servlet-name >XFireServlet </ servlet-name >
< servlet-class >org.codehaus.xfire.transport.http.XFireConfigurableServlet </ servlet-class >
< load-on-startup >0 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >XFireServlet </ servlet-name >
< url-pattern >/services/* </ url-pattern >
</ servlet-mapping >
< welcome-file-list >
< welcome-file >index.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
< web-app xmlns ="http://java.sun.com/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.5" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]" >
< servlet >
< servlet-name >XFireServlet </ servlet-name >
< servlet-class >org.codehaus.xfire.transport.http.XFireConfigurableServlet </ servlet-class >
< load-on-startup >0 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >XFireServlet </ servlet-name >
< url-pattern >/services/* </ url-pattern >
</ servlet-mapping >
< welcome-file-list >
< welcome-file >index.jsp </ welcome-file >
</ welcome-file-list >
</ web-app >
2、创建WebService服务
创建两个个包“wstest.server”和“wstest.client”,用来保存服务端和客户端程序。然后开始创建服务端程序,如下图
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
< beans xmlns ="http://xfire.codehaus.org/config/1.0" >
< service >
< name >MyService </ name >
< serviceClass >wstest.server.IMyService </ serviceClass >
< implementationClass >
wstest.server.MyServiceImpl
</ implementationClass >
< style >wrapped </ style >
< use >literal </ use >
< scope >application </ scope >
</ service >
</ beans >
< beans xmlns ="http://xfire.codehaus.org/config/1.0" >
< service >
< name >MyService </ name >
< serviceClass >wstest.server.IMyService </ serviceClass >
< implementationClass >
wstest.server.MyServiceImpl
</ implementationClass >
< style >wrapped </ style >
< use >literal </ use >
< scope >application </ scope >
</ service >
</ beans >
package wstest.server;
//Generated by MyEclipse
public interface IMyService {
public String sayHello(String user);
}
//Generated by MyEclipse
public interface IMyService {
public String sayHello(String user);
}
package wstest.server;
//Generated by MyEclipse
public class MyServiceImpl implements IMyService {
public String sayHello(String user) {
return "您好,"+user;
}
}
//Generated by MyEclipse
public class MyServiceImpl implements IMyService {
public String sayHello(String user) {
return "您好,"+user;
}
}
至此,服务端代码已经完成。
3、测试服务端代码
测试依赖与Servlet容器Tomcat,需要将做好的服务端打包部署到tomcat上,然后启动。才可以进行测试。假设你已经配置了Tomcat服务器,并完成了WebService服务端的部署。那么,现在就启动Tomcat,然后:
这样,出现上上面的结果,表明测试成功了。
但是,这就是MyEclipse的功能,我改变不了。
5、客户端测试
下面就耐心看怎么用这个客户端代码。
package wstest.client;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import javax.xml.namespace.QName;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;
public class MyServiceClient {
private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
private HashMap endpoints = new HashMap();
private Service service0;
public MyServiceClient() {
create0();
Endpoint MyServicePortTypeLocalEndpointEP = service0 .addEndpoint( new QName( "http://server.wstest","MyServicePortTypeLocalEndpoint"),new QName("http://server.wstest","MyServicePortTypeLocalBinding"),"xfire.local://MyService");
endpoints.put(new QName("http://server.wstest",MyServicePortTypeLocalEndpointEP);
Endpoint MyServiceHttpPortEP = service0 .addEndpoint(new QName("http://server.wstest","MyServiceHttpPort"),"MyServiceHttpBinding"),"http://localhost:8080/xfire126Demo/services/MyService");
endpoints.put(new QName("http://server.wstest",MyServiceHttpPortEP);
}
public Object getEndpoint(Endpoint endpoint) {
try {
return proxyFactory.create((endpoint).getBinding(),(endpoint).getUrl());
} catch (MalformedURLException e) {
throw new XFireRuntimeException("Invalid URL",e);
}
}
public Object getEndpoint(QName name) {
Endpoint endpoint = ((Endpoint) endpoints.get((name)));
if ((endpoint) == null) {
throw new IllegalStateException("No such endpoint!");
}
return getEndpoint((endpoint));
}
public Collection getEndpoints() {
return endpoints.values();
}
private void create0() {
TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
HashMap props = new HashMap();
props.put("annotations.allow.interface",true);
AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(),tm,new AegisBindingProvider(new JaxbTypeRegistry()));
asf.setBindingCreationEnabled(false);
service0 = asf.create((wstest.client.MyServicePortType.class),props);
{
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0,new QName("http://server.wstest","http://schemas.xmlsoap.org/soap/http");
}
{
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0,"urn:xfire:transport:local");
}
}
public MyServicePortType getMyServicePortTypeLocalEndpoint() {
return ((MyServicePortType)(this).getEndpoint(new QName("http://server.wstest","MyServicePortTypeLocalEndpoint")));
}
public MyServicePortType getMyServicePortTypeLocalEndpoint(String url) {
MyServicePortType var = getMyServicePortTypeLocalEndpoint();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
}
public MyServicePortType getMyServiceHttpPort() {
return ((MyServicePortType)(this).getEndpoint(new QName("http://server.wstest","MyServiceHttpPort")));
}
public MyServicePortType getMyServiceHttpPort(String url) {
MyServicePortType var = getMyServiceHttpPort();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
}
public static void main(String[] args) {
MyServiceClient client = new MyServiceClient();
//create a default service endpoint
MyServicePortType service = client.getMyServiceHttpPort();
//Todo: Add custom client code here
//
//service.yourServiceOperationHere();
System.out.println("test client completed");
System.exit(0);
}
}
运行结果如下:
您好,熔岩
test client completed
test client completed
终于可以松一口气了。完整的例子跑起来了。
6、总结
总感觉这个开发过程不爽,其实有更好的工具和开发方式:
其实,只要服务端编写好了,就完全可以用别的方式根据wsdl的url去生成客户端代码,在这里不得不将一个强大的工具IDEA8推荐出来,IDEA8自带WebService开发工具,插件非常强大,易用。在后面的篇幅中,我会做专门介绍,敬请关注。
当然,MyEclipse也并非一无是处,MyEclipse的服务端调试工具就很不错,很喜欢。提高了开发效率,这也是MyEclipse的过人之处。
最后,告诫各位,即使WebService支持复杂对象参数,也不建议使用,因为数据绑定还不是那么完美,总有些缺憾,为了保险起见,还是建议使用String作为参数最好了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。