学习了一下WebService, 做了个小例子,以供参考:
1. 下载 apache-cxf-2.2.12.zip 包,地址: http://cxf.apache.org/
2. 创建 java 工程,导入相关的jar包.
3. 创建一个接口(使用注解声明webservice @WebService )
package test; import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHello(String name); }
4. 实现创建的接口
package test; import javax.jws.WebService; @WebService public class HelloWorldImpl implements HelloWorld { public String sayHello(String name) { // TODO Auto-generated method stub System.out.println("接口实现类!"); return "Hello "+name+" ! "; } }
5. 创建WebService服务器端
package test; import org.apache.cxf.endpoint.Server; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class MainServer { /** * 创建一个服务端 */ public static void main(String[] args) { // 创建一个WebService服务端工厂类 JaxWsServerFactoryBean factory =new JaxWsServerFactoryBean(); //定义webService具体实现类,而不是接口 factory.setServiceClass(HelloWorldImpl.class); //发布webServer到这个地址 factory.setAddress("http://localhost:9001/HelloWorld"); //得到Server,并启动 Server server= factory.create() ; server.start(); } }
启动服务器端程序, IE浏览器中 http://localhost:9001/HelloWorld?WSDL
可以看到, 服务器端正常
<?xml version="1.0" encoding="UTF-8" ?> - <wsdl:definitions name="HelloWorldImplService" targetNamespace="http://test/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> - <wsdl:types> - <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://test/" xmlns:tns="http://test/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="sayHello" type="tns:sayHello" /> - <xsd:complexType name="sayHello"> - <xsd:sequence> <xsd:element minOccurs="0" name="arg0" type="xsd:string" /> </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="sayHello"> <wsdl:part element="tns:sayHello" name="parameters" /> </wsdl:message> - <wsdl:message name="sayHelloResponse"> <wsdl:part element="tns:sayHelloResponse" name="parameters" /> </wsdl:message> - <wsdl:portType name="HelloWorld"> - <wsdl:operation name="sayHello"> <wsdl:input message="tns:sayHello" name="sayHello" /> <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" /> </wsdl:operation> </wsdl:portType> - <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld"> <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="HelloWorldImplService"> - <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort"> <soap:address location="http://localhost:9001/HelloWorld" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
6. 创建webService客户端
package test; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class HelloClient { /** * 创建WebService客户端 */ public static void main(String[] args) { // 创建代理工厂类(客户端) JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setAddress("http://localhost:9001/HelloWorld"); //客户端定义实现接口 factory.setServiceClass(HelloWorld.class); HelloWorld hello= (HelloWorld)factory.create(); System.out.println(hello.sayHello("中 国")); } }
最后启动服务器端程序MainServer,客户端HelloClient ,会显示
2011-7-24 17:42:00 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://test/}HelloWorldService from class test.HelloWorld Hello 中 国 !
此时WebService发布成功!