CXF用JDK注解的方式发布WebService简单例子

CXF用JDK注解的方式发布WebService简单例子

1. 新建工程,导入CXF相关包:

  • cxf-2.4.2.jar
  • jetty-continuation-7.4.5.v20110725.jar
  • jetty-http-7.4.5.v20110725.jar
  • jetty-io-7.4.5.v20110725.jar
  • jetty-security-7.4.5.v20110725.jar
  • jetty-server-7.4.5.v20110725.jar
  • jetty-util-7.4.5.v20110725.jar
  • neethi-3.0.1.jar
  • wsdl4j-1.6.2.jar
  • xmlschema-core-2.0.jar 


2. UserService服务端代码:

package com.cxf.test;
 import javax.jws.WebService;
 @WebService
 public class UserService
 {
     public String addUser(String name, String age)
     {
         return "add user with " + name + " and " + age;
     }
     public int deleteUser(String id)
     {
         return id.length();
     }
 }

这是通过JDK注解的方式。Annotation


3. 发布UserService,代码如下:

package com.cxf.test;
 import javax.xml.ws.Endpoint;
 public class DeployUserService
 {
     public static void main(String[] args)
     {
         Endpoint.publish("http://localhost:9002/user", new UserService());
     }
 }


4. 运行结果如下:

控制台:

2012-9-20 15:12:34 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
 信息: Creating Service {http://test.cxf.com/}UserServiceService from class com.cxf.test.UserService
 2012-9-20 15:12:34 org.apache.cxf.endpoint.ServerImpl initDestination
 信息: Setting the server's publish address to be http://localhost:9002/user
 2012-09-20 15:12:34.659:INFO::jetty-7.4.5.v20110725
 2012-09-20 15:12:34.737:INFO::Started SelectChannelConnector@localhost:9002 STARTING
 2012-09-20 15:12:34.752:INFO::started o.e.j.s.h.ContextHandler{,null}


访问http://localhost:9002/user?wsdl 查看WS是否发布结果。

<?xml version="1.0" ?> 
 - <wsdl:definitions name="UserServiceService" targetNamespace="http://test.cxf.com/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test.cxf.com/" 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.cxf.com/" xmlns:tns="http://test.cxf.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="deleteUser" type="tns:deleteUser" /> 
 - <xsd:complexType name="deleteUser">
 - <xsd:sequence>
   <xsd:element minOccurs="0" name="arg0" type="xsd:string" /> 
   </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="deleteUserResponse" type="tns:deleteUserResponse" /> 
 - <xsd:complexType name="deleteUserResponse">
 - <xsd:sequence>
   <xsd:element name="return" type="xsd:int" /> 
   </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="addUser" type="tns:addUser" /> 
 - <xsd:complexType name="addUser">
 - <xsd:sequence>
   <xsd:element minOccurs="0" name="arg0" type="xsd:string" /> 
   <xsd:element minOccurs="0" name="arg1" type="xsd:string" /> 
   </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="addUserResponse" type="tns:addUserResponse" /> 
 - <xsd:complexType name="addUserResponse">
 - <xsd:sequence>
   <xsd:element minOccurs="0" name="return" type="xsd:string" /> 
   </xsd:sequence>
   </xsd:complexType>
   </xsd:schema>
   </wsdl:types>
 - <wsdl:message name="addUserResponse">
   <wsdl:part element="tns:addUserResponse" name="parameters" /> 
   </wsdl:message>
 - <wsdl:message name="deleteUserResponse">
   <wsdl:part element="tns:deleteUserResponse" name="parameters" /> 
   </wsdl:message>
 - <wsdl:message name="deleteUser">
   <wsdl:part element="tns:deleteUser" name="parameters" /> 
   </wsdl:message>
 - <wsdl:message name="addUser">
   <wsdl:part element="tns:addUser" name="parameters" /> 
   </wsdl:message>
 - <wsdl:portType name="UserService">
 - <wsdl:operation name="deleteUser">
   <wsdl:input message="tns:deleteUser" name="deleteUser" /> 
   <wsdl:output message="tns:deleteUserResponse" name="deleteUserResponse" /> 
   </wsdl:operation>
 - <wsdl:operation name="addUser">
   <wsdl:input message="tns:addUser" name="addUser" /> 
   <wsdl:output message="tns:addUserResponse" name="addUserResponse" /> 
   </wsdl:operation>
   </wsdl:portType>
 - <wsdl:binding name="UserServiceServiceSoapBinding" type="tns:UserService">
   <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
 - <wsdl:operation name="deleteUser">
   <soap:operation soapAction="" style="document" /> 
 - <wsdl:input name="deleteUser">
   <soap:body use="literal" /> 
   </wsdl:input>
 - <wsdl:output name="deleteUserResponse">
   <soap:body use="literal" /> 
   </wsdl:output>
   </wsdl:operation>
 - <wsdl:operation name="addUser">
   <soap:operation soapAction="" style="document" /> 
 - <wsdl:input name="addUser">
   <soap:body use="literal" /> 
   </wsdl:input>
 - <wsdl:output name="addUserResponse">
   <soap:body use="literal" /> 
   </wsdl:output>
   </wsdl:operation>
   </wsdl:binding>
 - <wsdl:service name="UserServiceService">
 - <wsdl:port binding="tns:UserServiceServiceSoapBinding" name="UserServicePort">
   <soap:address location="http://localhost:9002/user" /> 
   </wsdl:port>
   </wsdl:service>
   </wsdl:definitions>


5.  新建客户端:导入如下包:

  • neethi-3.0.1.jar
  • cxf-2.4.2.jar
  • wsdl4j-1.6.2.jar
  • xmlschema-core-2.0.jar

6. 定制客户端调用WebService的接口,这个接口中的方法签名和参数信息可以从wsdl中的内容看到,代码如下:

package com.cxf.test;
 import javax.jws.WebService;
 @WebService
 public interface IUser
 {
     String addUser(String name, String age);
     int deleteUser(String id);
 }
 


7. 编写客户端调用WebService代码

package com.cxf.test;
 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 public class UserServiceClient
 {
     public static void main(String[] args)
     {
         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
         factory.setAddress("http://localhost:9002/user");
         factory.setServiceClass(IUser.class);
         IUser user = (IUser) factory.create();
         
         System.out.println(user.addUser("hello", "world"));
         System.out.println(user.deleteUser("ssss"));
     }
 }

8. 运行结果如下:

2012-9-20 15:14:36 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
 信息: Creating Service {http://test.cxf.com/}IUserService from class com.cxf.test.IUser
 add user with hello and world
 4

posted @ 2012-09-20 21:54  mylitboy  阅读(719)  评论(0编辑  收藏  举报