在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>
。。。。。。。。。。。。。。。。。。。。。。。。。。。太长了,我就不全部复制
客户端
我写的客户端需要用到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也只停留在这个程度了,暂时还没有深入了解。