使用CXF发布WebService服务简单实例

一、说明:

前面介绍了使用axis2来发布Webservice服务,现在介绍一种更popular,更高效的Webservice服务发布技术:CXF

Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。目前它仍只是 Apache 的一个孵化项目。

Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

CXF 框架是一种基于 Servlet 技术的 SOA 应用开发框架,要正常运行基于 CXF 应用框架开发的企业应用,除了 CXF 框架本身之外,还需要 JDK 和 Servlet 容器的支持。

二、利用CXF进行简单webservice服务的发布与调用

①.  新建一个Java Project  ,CXFDemo ,并在工程下新建lib文件夹,加入CXF的jar包:

 
  1. cxf-core-3.1.4.jar

  2. jaxb-core-2.2.11.jar

  3. jaxb-impl-2.2.11.jar

  4. neethi-3.0.3.jar

  5. wsdl4j-1.6.3.jar

  6. xmlschema-core-2.2.1.jar

然后build path ,将架包加入到类路径下。

②.  在src下,新建3个package ,并分别在各个包下创建对应的相关接口和类:

com.elgin.cxf.entities
com.elgin.cxf.service
com.elgin.cxf.service.impl

 ❶、在service包下新建接口  HelloService

 
  1. package com.elgin.cxf.service;

  2.  
  3. import javax.jws.WebParam;

  4. import javax.jws.WebService;

  5.  
  6. import com.elgin.cxf.entities.User;

  7.  
  8. @WebService

  9. public interface HelloService {

  10.  
  11. public String sayHello(@WebParam(name="text")String text);

  12.  
  13. public String sayHellloToUser(User user);

  14. }

❷、在service.impl 包下新建接口的实现类 HelloServiceImpl

 
  1. package com.elgin.cxf.service.impl;

  2.  
  3. import javax.jws.WebService;

  4.  
  5. import com.elgin.cxf.entities.User;

  6. import com.elgin.cxf.service.HelloService;

  7.  
  8. @WebService(endpointInterface="com.elgin.cxf.service.HelloService",serviceName="HelloService")

  9. public class HelloServiceImpl implements HelloService {

  10.  
  11. @Override

  12. public String sayHello(String text) {

  13. System.out.println("param text is :" + text);

  14. return "hello " + text;

  15. }

  16.  
  17. @Override

  18. public String sayHellloToUser(User user) {

  19. System.out.println("param user is :" +user);

  20. return "hello " + user.getName();

  21. }

  22.  
  23. }

❸、entities包下新建用到的User类:

 
  1. package com.elgin.cxf.entities;

  2.  
  3. public class User {

  4. private String name;

  5.  
  6. public String getName() {

  7. return name;

  8. }

  9.  
  10. public void setName(String name) {

  11. this.name = name;

  12. }

  13.  
  14. public User(){}

  15.  
  16. public User(String name) {

  17. super();

  18. this.name = name;

  19. }

  20.  
  21. @Override

  22. public String toString() {

  23. return "User [name=" + name + "]";

  24. }

  25.  
  26. }

③.  新建package  :com.elgin.cxf.publish

并在此包下新建类: Server  用来发布webservice服务:

 
  1. package com.elgin.cxf.publish;

  2.  
  3. import javax.xml.ws.Endpoint;

  4.  
  5. import com.elgin.cxf.service.impl.HelloServiceImpl;

  6.  
  7. public class Server {

  8. public static void main(String[] args) {

  9. System.out.println("cxf service start..");

  10. HelloServiceImpl serviceImpl=new HelloServiceImpl();

  11. String address="http://localhost:8080/service/hello";

  12. Endpoint.publish(address, serviceImpl);

  13. }

  14. }

④  运行查看发布情况:

在浏览器输入:http://localhost:8080/service/hello?wsdl ,出现下图所示数据,表示发布webservice服务成功

⑤ 服务调用代码以及结果:

 
  1. package com.elgin.cxf.client;

  2.  
  3.  
  4. import java.net.URL;

  5.  
  6. import javax.xml.namespace.QName;

  7. import javax.xml.ws.Service;

  8. import javax.xml.ws.soap.SOAPBinding;

  9.  
  10. import com.elgin.cxf.entities.User;

  11. import com.elgin.cxf.service.HelloService;

  12.  
  13. public class Client {

  14. private static final QName SERVICE_NAME

  15. = new QName("http://impl.service.cxf.elgin.com/", "HelloService");

  16. private static final QName PORT_NAME

  17. = new QName("http://impl.service.cxf.elgin.com/", "HelloServicePort");

  18.  
  19.  
  20. private Client() {}

  21.  
  22. public static void main(String args[]) throws Exception {

  23. rightInvoker();

  24. exceptionInvoker();

  25. }

  26.  
  27. /*

  28. * @Title: rightInvoker

  29. * @Description: 可成功调用,返回正确的结果

  30. * @throws Exception 参数

  31. */

  32. public static void rightInvoker() throws Exception {

  33. // Endpoint Address

  34. String endpointAddress = "http://localhost:8080/service/hello";

  35. URL url=new URL(endpointAddress);

  36. Service service = Service.create(url,SERVICE_NAME);

  37.  
  38. HelloService hs = service.getPort(HelloService.class);

  39. System.out.println(hs.sayHello("World"));

  40.  
  41. User user = new User("Jack");

  42. System.out.println(hs.sayHellloToUser(user));

  43. }

  44.  
  45. /*

  46. * @Title: exceptionInvoker

  47. * @Description: 调用时出现异常错误信息:

  48. *

  49. * Exception in thread "main" javax.xml.ws.WebServiceException:

  50. * WSDL Metadata not available to create the proxy,

  51. * either Service instance or ServiceEndpointInterface com.elgin.cxf.service.HelloService should have WSDL information

  52. *

  53. * 对于造成上述异常的具体原因尚不明确,很有可能是缺少CXF某个jar包引起

  54. */

  55. public static void exceptionInvoker(){

  56. // Endpoint Address

  57. Service service = Service.create(SERVICE_NAME);

  58. String endpointAddress = "http://localhost:8080/service/hello";

  59. service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);

  60. HelloService hs = service.getPort(HelloService.class);

  61. System.out.println(hs.sayHello("World"));

  62.  
  63. User user = new User("Jack");

  64. System.out.println(hs.sayHellloToUser(user));

  65. }

  66. }

  67.  
  68.  
  69.  

正确运行结果:

hello World

猜你喜欢

转载自blog.csdn.net/u014203489/article/details/82416292