通过myeclipse 用xfire快速配置web service服务端和客户端

在myeclipse 中,选择new,再选择web service project,输入project name之后,在Framework中选XFire,点击确定之后,一个简易的web service架构已经形成了。

下面我们需要做的就是添加services.xml的相关配置(文件已经生成了),添加响应的接口、类。

先加接口

package com;
import java.util.List;
public interface TestI {
public String sayHello(String ttt);
public Course choose(User u);
public List tester(List t);
}


再加实现类

package com;
import java.util.ArrayList;
import java.util.List;
public class Test implements TestI {
public String sayHello(String ttt) {
return "Hello, "+ttt;
}
public Course choose(User u){
// System.out.println(u.getName());
Course c=new Course();
c.setName(u.getName());
return c;

}

public List tester(List t){
for (int i = 0; i < t.size(); i++) {
System.out.println((String) t.get(i));
}
List al=new ArrayList();
Course c=new Course();
c.setName("EeeDDDDDD");
al.add(c);
return al;

}
}



其它相关的类Course.java和User.java和TestI.aegis.xml

package com;

public class Course {
public Course(){}
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}



package com;
public class User {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}




<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<mapping>
<method name="tester">
<parameter index="0" componentType="java.lang.String" />
<return-type componentType="com.Course" />
</method>
</mapping>
</mappings>


services.xml文件需要配置一下,如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>HelloService</name>
<namespace>sssssssssss</namespace>
<serviceClass>com.TestI</serviceClass>
<implementationClass>com.Test</implementationClass>

</service>

</beans>


我用的myeclipse自带的JDK和Tomcat,这样就可以正常启动了。
我用http://localhost:8080/项目名称/services/HelloService?wsdl就可以打开web service的服务端wdsl。

类似于这样子:


<wsdl:definitions targetNamespace="sssssssssss">

<wsdl:types>

<xsd:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="sssssssssss">

<xsd:complexType name="ArrayOfString">

<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="string" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>

<xsd:element name="tester">

<xsd:complexType>

<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="tns:ArrayOfString"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:element name="testerResponse">

<xsd:complexType>

<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="ns1:ArrayOfCourse"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
。。。。。。。。。。。。。。。。。。。。。。。。。。。太长了,我就不全部复制


[b]
客户端
[/b]
我写的客户端需要用到xfire的包,在这里输入参数,可以得到你预期的结果,这正是我们想要得到的。
代码如下:

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

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

import com.Course;
import com.TestI;
import com.User;
public class Client {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Service srvcModel = new ObjectServiceFactory()
.create(TestI.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
String helloWorldURL = "http://localhost:8080/cxf/services/HelloService";
try {
TestI srvc = (TestI) factory.create(srvcModel,
helloWorldURL);
// System.out.println(srvc.getNum(5, 8, 6));
System.out.println(srvc.sayHello("Robin"));

User u=new User();
u.setName("RRRRR");
Course c=srvc.choose(u);
System.out.println("2. "+c.getName());

List al=new ArrayList();
al.add("1212");
al.add("2222");
List t=srvc.tester(al);
for (int i = 0; i < t.size(); i++) {
Course co=(Course)t.get(i);
System.out.println(co.getName());
}


} catch (MalformedURLException e) {
e.printStackTrace();
}


}

}




本来我的项目需要用到的,但现在用其它方法了,我的web service也只停留在这个程度了,暂时还没有深入了解。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值