1. 开发服务接口
package com.yanek.demo.webservice;
public interface IHelloWorldService {
public String sayHello(String name);
public String[] getUsers();
public User[] getUserList();
}
2. 开发开发服务接口实现类
package com.yanek.demo.webservice;
public class HelloWorldServiceImpl implements IHelloWorldService {
public String sayHello(String name){
return "hello,"+name;
}
public String[] getUsers(){
String[] users=new String[2];
users[0]="yanek";
users[1]="aspboy";
return users;
}
public User[] getUserList(){
User u1=new User();
u1.setAge(1);
u1.setUsername("u1");
User u2=new User();
u2.setAge(2);
u2.setUsername("u2");
User[] users=new User[2];
users[0]=u1;
users[1]=u2;
return users;
}
}
相关类
package com.yanek.demo.webservice;
public class User implements java.io.Serializable
{
private int age;
private String username;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
3. web.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
</web-app>
4. services.xml文件
配置接口和实现类的名称
路径:classes/META-INF/xfire/services.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>HelloWorldService</name>
<serviceClass>
com.yanek.demo.webservice.IHelloWorldService
</serviceClass>
<implementationClass>
com.yanek.demo.webservice.HelloWorldServiceImpl
</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
</beans>
部署后测试服务是否启动
访问:http://helloworld.myframework.com:8099/services/HelloWorldService?wsdl
如下 则成功发布!
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://webservice.demo.yanek.com" xmlns:tns="http://webservice.demo.yanek.com" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://webservice.demo.yanek.com" elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:element name="getUserList">
<xsd:complexType/>
</xsd:element>
<xsd:complexType name="ArrayOfUser">
<xsd:sequence>
<xsd:element name="User" type="tns:User" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="User">
<xsd:sequence>
<xsd:element name="age" type="xsd:int" minOccurs="0"/>
<xsd:element name="username" type="xsd:string" minOccurs="0" nillable="true"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="getUserListResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="tns:ArrayOfUser" nillable="true" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="getUsers">
<xsd:complexType/>
</xsd:element>
<xsd:complexType name="ArrayOfString">
<xsd:sequence>
<xsd:element name="string" type="xsd:string" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="getUsersResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="tns:ArrayOfString" nillable="true" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sayHello">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in0" type="xsd:string" nillable="true" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sayHelloResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:string" nillable="true" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="getUserListResponse">
<wsdl:part name="parameters" element="tns:getUserListResponse"/>
</wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="tns:sayHelloResponse"/>
</wsdl:message>
<wsdl:message name="getUserListRequest">
<wsdl:part name="parameters" element="tns:getUserList"/>
</wsdl:message>
<wsdl:message name="getUsersResponse">
<wsdl:part name="parameters" element="tns:getUsersResponse"/>
</wsdl:message>
<wsdl:message name="getUsersRequest">
<wsdl:part name="parameters" element="tns:getUsers"/>
</wsdl:message>
<wsdl:message name="sayHelloRequest">
<wsdl:part name="parameters" element="tns:sayHello"/>
</wsdl:message>
<wsdl:portType name="HelloWorldServicePortType">
<wsdl:operation name="getUserList">
<wsdl:input name="getUserListRequest" message="tns:getUserListRequest"/>
<wsdl:output name="getUserListResponse" message="tns:getUserListResponse"/>
</wsdl:operation>
<wsdl:operation name="getUsers">
<wsdl:input name="getUsersRequest" message="tns:getUsersRequest"/>
<wsdl:output name="getUsersResponse" message="tns:getUsersResponse"/>
</wsdl:operation>
<wsdl:operation name="sayHello">
<wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest"/>
<wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse"/>
</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="getUserList">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getUserListRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getUserListResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getUsers">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getUsersRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getUsersResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<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://helloworld.myframework.com:8099/services/HelloWorldService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
5.本地测试:
测试代码和服务代码在相同系统中
测试中使用服务中的类
package com.yanek.demo.webservice;
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;
public class HelloWorldClient {
public static void main(String args[]) {
Service srvcModel = new ObjectServiceFactory()
.create(IHelloWorldService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
//wsdl: http://helloworld.myframework.com:8099/services/HelloWorldService?wsdl
//String helloWorldURL = "http://localhost:8081/HelloWorld/services/HelloWorldService";
String helloWorldURL = "http://helloworld.myframework.com:8099/services/HelloWorldService";
//http://helloworld.myframework.com:8099/services/HelloWorldService?wsdl
try {
IHelloWorldService srvc = (IHelloWorldService) factory.create(
srvcModel, helloWorldURL);
System.out.println(srvc.sayHello("yanek"));
String[] users=srvc.getUsers();
for(int i=0;i<users.length;i++)
{
System.out.println(users[i]);
}
User[] users1=srvc.getUserList();
for(int i=0;i<users1.length;i++)
{
System.out.println(users1[i].getUsername());
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
在其他系统
中调用
一. 使用jar包,使用代理类调用
1. 把相关提供服务的类打包hello.jar
2. 调用系统中按照如上代理类调用
package com.yanek.demo.webservice;
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;
public class HelloWorldClient {
public static void main(String args[]) {
Service srvcModel = new ObjectServiceFactory()
.create(IHelloWorldService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
//wsdl: http://helloworld.myframework.com:8099/services/HelloWorldService?wsdl
//String helloWorldURL = "http://localhost:8081/HelloWorld/services/HelloWorldService";
String helloWorldURL = "http://helloworld.myframework.com:8099/services/HelloWorldService";
//http://helloworld.myframework.com:8099/services/HelloWorldService?wsdl
try {
IHelloWorldService srvc = (IHelloWorldService) factory.create(
srvcModel, helloWorldURL);
System.out.println(srvc.sayHello("yanek"));
String[] users=srvc.getUsers();
for(int i=0;i<users.length;i++)
{
System.out.println(users[i]);
}
User[] users1=srvc.getUserList();
for(int i=0;i<users1.length;i++)
{
System.out.println(users1[i].getUsername());
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
二 根据wsdl使用工具生成客户端代理类
eclipse插件
按照插件生产的代码调用
file-->new-->webservice-->webservice client
http://helloworld.myframework.com:8099/services/HelloWorldService?wsdl
相关jar包: