首先定义个接口Hello.java
Java代码
package ch1;
public interface Hello {
public String sayHello();
}
package ch1;
public interface Hello {
public String sayHello();
} 然后实现这个接口, 实现类为:HelloImpl.java
Java代码
package ch1;
public class HelloImpl implements Hello{
public String sayHello() {
return "hello";
}
}
package ch1;
public class HelloImpl implements Hello{
public String sayHello() {
return "hello";
}
} 通过CXF把这个web service发表出去:
package ch1;
Java代码
import org.apache.cxf.frontend.ServerFactoryBean;
public class Test {
public static void main(String[] args)
{
HelloImpl impl = new HelloImpl();
ServerFactoryBean factory = new ServerFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
factory.setServiceBean(impl);factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.create();
}
}
import org.apache.cxf.frontend.ServerFactoryBean;
public class Test {
public static void main(String[] args)
{
HelloImpl impl = new HelloImpl();
ServerFactoryBean factory = new ServerFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
factory.setServiceBean(impl);factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.create();
}
} 访问这个web service的wsdl文件 http://localhost:8080/Hello?wsdl
Html代码
<wsdl:definitions name="Hello" targetNamespace="http://ch1/">
<wsdl:types>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://ch1/">
<xsd:element name="sayHello" type="tns:sayHello"/>
<xsd:complexType name="sayHello">
<xsd:sequence/>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xsd:complexType name="sayHelloResponse">
<xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloPortType">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloSoapBinding" type="tns:HelloPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Hello">
<wsdl:port binding="tns:HelloSoapBinding" name="HelloPort">
<soap:address location="http://localhost:8080/Hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
<wsdl:definitions name="Hello" targetNamespace="http://ch1/">
<wsdl:types>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://ch1/">
<xsd:element name="sayHello" type="tns:sayHello"/>
<xsd:complexType name="sayHello">
<xsd:sequence/>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xsd:complexType name="sayHelloResponse">
<xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloPortType">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloSoapBinding" type="tns:HelloPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Hello">
<wsdl:port binding="tns:HelloSoapBinding" name="HelloPort">
<soap:address location="http://localhost:8080/Hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
web service的服务器端把这个wsdl暴露出来, 客户端可以根据这个wsdl来访问客户端。
客户端通过ClientProxyFactoryBean类创建实例, 然后调用实例的方法。
Java代码
public class TestClient
{
public static void main(String[] args)
{
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
Hello hello = (Hello) factory.create();
System.out.println("invoke sayHello()...");
hello.sayHello();
System.exit(0);
}
}
public class TestClient
{
public static void main(String[] args)
{
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
Hello hello = (Hello) factory.create();
System.out.println("invoke sayHello()...");
hello.sayHello();
System.exit(0);
}
} 运行这个java文件可以在客户端看到结果:
invoke sayHello()...
hello
这样我们就可以访问这个发表出去的web service了
也可以在eclipse 的 run->Launch the web service Explorer 来观察已经发表的web servic
还可以添加拦截器
Java代码
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getInInterceptors().add(new LoggingInInterceptor());Java代码
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
Java代码
package ch1;
public interface Hello {
public String sayHello();
}
package ch1;
public interface Hello {
public String sayHello();
} 然后实现这个接口, 实现类为:HelloImpl.java
Java代码
package ch1;
public class HelloImpl implements Hello{
public String sayHello() {
return "hello";
}
}
package ch1;
public class HelloImpl implements Hello{
public String sayHello() {
return "hello";
}
} 通过CXF把这个web service发表出去:
package ch1;
Java代码
import org.apache.cxf.frontend.ServerFactoryBean;
public class Test {
public static void main(String[] args)
{
HelloImpl impl = new HelloImpl();
ServerFactoryBean factory = new ServerFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
factory.setServiceBean(impl);factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.create();
}
}
import org.apache.cxf.frontend.ServerFactoryBean;
public class Test {
public static void main(String[] args)
{
HelloImpl impl = new HelloImpl();
ServerFactoryBean factory = new ServerFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
factory.setServiceBean(impl);factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.create();
}
} 访问这个web service的wsdl文件 http://localhost:8080/Hello?wsdl
Html代码
<wsdl:definitions name="Hello" targetNamespace="http://ch1/">
<wsdl:types>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://ch1/">
<xsd:element name="sayHello" type="tns:sayHello"/>
<xsd:complexType name="sayHello">
<xsd:sequence/>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xsd:complexType name="sayHelloResponse">
<xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloPortType">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloSoapBinding" type="tns:HelloPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Hello">
<wsdl:port binding="tns:HelloSoapBinding" name="HelloPort">
<soap:address location="http://localhost:8080/Hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
<wsdl:definitions name="Hello" targetNamespace="http://ch1/">
<wsdl:types>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://ch1/">
<xsd:element name="sayHello" type="tns:sayHello"/>
<xsd:complexType name="sayHello">
<xsd:sequence/>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xsd:complexType name="sayHelloResponse">
<xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloPortType">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloSoapBinding" type="tns:HelloPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Hello">
<wsdl:port binding="tns:HelloSoapBinding" name="HelloPort">
<soap:address location="http://localhost:8080/Hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
web service的服务器端把这个wsdl暴露出来, 客户端可以根据这个wsdl来访问客户端。
客户端通过ClientProxyFactoryBean类创建实例, 然后调用实例的方法。
Java代码
public class TestClient
{
public static void main(String[] args)
{
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
Hello hello = (Hello) factory.create();
System.out.println("invoke sayHello()...");
hello.sayHello();
System.exit(0);
}
}
public class TestClient
{
public static void main(String[] args)
{
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
Hello hello = (Hello) factory.create();
System.out.println("invoke sayHello()...");
hello.sayHello();
System.exit(0);
}
} 运行这个java文件可以在客户端看到结果:
invoke sayHello()...
hello
这样我们就可以访问这个发表出去的web service了
也可以在eclipse 的 run->Launch the web service Explorer 来观察已经发表的web servic
还可以添加拦截器
Java代码
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getInInterceptors().add(new LoggingInInterceptor());Java代码
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
发表评论
-
Ant中build.XML使用详解
2011-11-29 15:29 1001Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它 ... -
java开源框架总结
2011-09-20 19:47 3002Java开源框架集 链接http: ... -
JXL 数据库向Excel导入数据
2011-09-16 13:10 1021package com.neusoft.serviceImp; ... -
jxl excel向数据库导入数据
2011-09-15 21:08 1959第一步: /** * 得到导入的excel数据并进 ... -
eclipse好习惯
2011-09-12 19:41 899http://developer.51cto.com/art/ ... -
jdeclipse按F3反编译
2011-07-21 10:53 1722使用Eclipse进行开发时,由于很多类是class文件,想看 ... -
hashmap
2011-07-10 17:03 1113今天看了篇文章,觉得 ...
相关推荐
C#Web Service C#Web Service C#Web Service
XML Web Service 开发XML Web Service 开发XML Web Service 开发XML Web Service 开发XML Web Service 开发最好不要下
1.Web service經典開發文檔! 2.它是一個軟件系統﹐為了支持跨網絡的機器間相相互交互而設計。Web Service服務通常被定義為一組模塊化的API﹐它們可以通過網絡進行調用﹐來執行遠程系統的請求服務。 3.XFire 是 ...
Java Web Service 学习例子,里面有大量用来开发web services的Java程序示例,非常值得学习。 使用到的技术及工具如下: Apache: www.apache.org and xml.apache.org -- Ant 1.4.1 -- SOAP 2.2 -- Tomcat 3.2...
service now web service guide
D6 SOAP_WEB SERVICE 使用delphi来开发 webservice 使用soap
webservice 从表面上看Web service 就是一个应用程序它向外界暴露出一个能够通过Web进行调用的API。这就是说你能 够用编程的方法通过Web来调用这个应用程序。 对Web service 更精确的解释 Web ...
远程抄表监控终端软件设计主要包括数据采集和Web Service的设计和实现,重点是使用gsoap软件包设计了供客户端调用的Web Service,并详细论述了远程抄表监控终端与客户端的通信。设计好的远程抄表监控终端作为一个...
Web Service和Grid Service研究,郭璇,,本文分析并比较了两种主流的Web服务技术,Web Service技术和Grid Service技术。首先介绍Web Service 的系统架构和核心技术,并深入探讨Web Servic
Query as a Web Service 指南
《实战Delphi6/Kylix2/SOAP/Web Service程序设计篇》(李维著)
二、 Web Service服务器端的发布。 2.1 创建java类。 2.2 创建services.xml。 2.3 发布服务。 2.4 在浏览器上测试服务。 三、 Web Service 客户端的创建。 3.1 创建WSDL文件。 3.2 利用WSDL创建stub。 3.3 利用stub...
Web Service编程--用C#.NET开发网络服务(1C 作者:陆昆仑,李旭东,吴缓静 编著 出版社:希望电子 出版时间:2003年04月 第一章 .NET和.NET框架 第二章 C#基本语法 第三章 面向对象的C# 第四章 C#高级...
T100接口开发-T100 Web Service 接口开发v1.5版最新版 WebService接口資料
web service discovery
delphi 6/Kylik2 soap/Web Service程序设计篇下册+代码,非常好的教程。
Delphi6 SOAP Web Service程序设计篇
Web Service 简单实例,JAVA中的Web服务规范等,完整的客户端于服务端的开发流程。
西北某高等院校研究生的web service课件,英文版的,总共7个ppt,比较简练,适合想快速了解学习web service的学生下载学习,分别是Web Services(ch3.1)、Web Services(ch3.2)、Web Services(ch3.3)、Web Services(ch...
T100 Web Service开发,调试整合重启检查说明(简体版)