zoukankan      html  css  js  c++  java
  • Apache CXF JAX-WS example

    1. 环境说明

    jdk 1.6.0_29

    apache cxf  2.7.7

    2. 新建Java Project

    3. 添加jar包,将apache cxf下面lib里面的jar包都添加到项目中(可能有些是不需要的,为了简单没进行筛选)。

    4. 项目目录结构:

    5. 代码如下

    Book.Java

    [java] view plain copy
     
    1. package com.unei.bean;  
    2.   
    3. import javax.xml.bind.annotation.XmlRootElement;  
    4.   
    5. @XmlRootElement(name = "Book")  
    6. public class Book {  
    7.     private int bookId;  
    8.     private String bookISBNnumber;  
    9.     private String bookName;  
    10.     private double price;  
    11.     private String author;  
    12.   
    13.     public double getPrice() {  
    14.         return price;  
    15.     }  
    16.   
    17.     public void setPrice(double price) {  
    18.         this.price = price;  
    19.     }  
    20.   
    21.     public int getBookId() {  
    22.         return bookId;  
    23.     }  
    24.   
    25.     public void setBookId(int bookId) {  
    26.         this.bookId = bookId;  
    27.     }  
    28.   
    29.     public String getBookISBNnumber() {  
    30.         return bookISBNnumber;  
    31.     }  
    32.   
    33.     public void setBookISBNnumber(String bookISBNnumber) {  
    34.         this.bookISBNnumber = bookISBNnumber;  
    35.     }  
    36.   
    37.     public String getBookName() {  
    38.         return bookName;  
    39.     }  
    40.   
    41.     public void setBookName(String bookName) {  
    42.         this.bookName = bookName;  
    43.     }  
    44.   
    45.     public String getAuthor() {  
    46.         return author;  
    47.     }  
    48.   
    49.     public void setAuthor(String author) {  
    50.         this.author = author;  
    51.     }  
    52. }  

    IBookService.java

    [java] view plain copy
     
    1. package com.unei.service;  
    2.   
    3. import javax.jws.WebMethod;  
    4. import javax.jws.WebService;  
    5.   
    6. import com.unei.bean.Book;  
    7.   
    8. @WebService  
    9. public interface IBookService {  
    10.     @WebMethod  
    11.     public void addBook(Book book);  
    12.     @WebMethod  
    13.     public void deleteBook(int bookId);  
    14.     @WebMethod  
    15.     public void updateBook(Book book);  
    16.     @WebMethod  
    17.     public Book getBook(int bookId);  
    18. }  

    BookService.java

    [java] view plain copy
     
    1. package com.unei.service.impl;  
    2.   
    3. import com.unei.bean.Book;  
    4. import com.unei.service.IBookService;  
    5.   
    6. public class BookService implements IBookService{  
    7.   
    8.     @Override  
    9.     public void addBook(Book book) {  
    10.         System.out.println("addBook:"+book.getBookName());  
    11.     }  
    12.   
    13.     @Override  
    14.     public void deleteBook(int bookId) {  
    15.         System.out.println("deleteBook:"+bookId);  
    16.     }  
    17.   
    18.     @Override  
    19.     public void updateBook(Book book) {  
    20.         System.out.println("updateBook:"+book.getBookName());  
    21.     }  
    22.   
    23.     @Override  
    24.     public Book getBook(int bookId) {  
    25.         Book book=new Book();  
    26.         book.setBookId(4);  
    27.         book.setBookName("getBook");  
    28.         book.setBookISBNnumber("ABCDEFG");  
    29.         book.setPrice(20.00);  
    30.         return book;  
    31.     }  
    32.   
    33. }  

    Server.java

    [java] view plain copy
     
    1. package com.unei.service.test;  
    2.   
    3.   
    4. import org.apache.cxf.interceptor.LoggingInInterceptor;  
    5. import org.apache.cxf.interceptor.LoggingOutInterceptor;  
    6. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
    7.   
    8.   
    9. import com.unei.service.IBookService;  
    10. import com.unei.service.impl.BookService;  
    11.   
    12.   
    13. public class Server {  
    14.     protected Server() throws Exception {  
    15.     <span style="white-space:pre">    </span>System.out.println("Starting server...");  
    16.     <span style="white-space:pre">    </span>BookService bs=new BookService();  
    17.     <span style="white-space:pre">    </span>JaxWsServerFactoryBean svrFactory=new JaxWsServerFactoryBean();  
    18.     <span style="white-space:pre">    </span>svrFactory.setServiceClass(IBookService.class);  
    19.     <span style="white-space:pre">    </span>svrFactory.setAddress("http://localhost:9000/bs");  
    20.     <span style="white-space:pre">    </span>svrFactory.setServiceBean(bs);  
    21.     <span style="white-space:pre">    </span>svrFactory.getInInterceptors().add(new LoggingInInterceptor());  
    22.     <span style="white-space:pre">    </span>svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());  
    23.     <span style="white-space:pre">    </span>svrFactory.create();  
    24.     }  
    25.   
    26.   
    27.     public static void main(String args[]) throws Exception {  
    28.         new Server();  
    29.         System.out.println("Server ready...");  
    30.   
    31.   
    32.         Thread.sleep(5 * 6000 * 1000);  
    33.         System.out.println("Server exiting");  
    34.         System.exit(0);  
    35.     }  
    36. }  

    6. 测试程序

    浏览器输入:http://localhost:9000/bs?wsdl

    浏览器输出wsdl文档

    7. 通过apache cxf生成客户端代码

    在命令提示符中运行:

    wsdl2java -frontend jaxws21 -p com.unei.soap.client -d F:ws http://localhost:9000/bs?wsdl

    将wsdl装换为java代码

    8. 新建Java Project,添加jar包,将cxf生成的客户端代码添加到项目中,项目如下:

    9. 客户端代码

    Client.java

    [java] view plain copy
     
      1. package com.unei.app;  
      2.   
      3. import org.apache.cxf.interceptor.LoggingInInterceptor;  
      4. import org.apache.cxf.interceptor.LoggingOutInterceptor;  
      5. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
      6.   
      7. import com.unei.soap.client.Book;  
      8. import com.unei.soap.client.IBookService;  
      9.   
      10. public class Client {  
      11.   
      12.     public static void main(String[] args) {  
      13.         JaxWsProxyFactoryBean factory=new JaxWsProxyFactoryBean();  
      14.         factory.getInInterceptors().add(new LoggingInInterceptor());  
      15.         factory.getOutInterceptors().add(new LoggingOutInterceptor());  
      16.         factory.setAddress("http://localhost:9000/bs");  
      17.         factory.setServiceClass(IBookService.class);  
      18.         Object o=factory.create();  
      19.         IBookService bs=(IBookService)o;  
      20.         Book b=new Book();  
      21.         b.setBookName("new book");  
      22.         b.setBookId(1);  
      23.         bs.addBook(b);  
      24.     }  
      25.   
      26. }  
  • 相关阅读:
    Python3 面向对象小练习
    Python3 面向对象进阶1
    Python3 类的继承小练习
    Python3 类的继承
    Python3 数据结构之词频统计(英文)
    Python3 类与对象之王者荣耀对战小游戏
    Python3 类与对象
    SQL优化单表案例
    SQL性能分析
    索引简介
  • 原文地址:https://www.cnblogs.com/zhuawang/p/6591037.html
Copyright © 2011-2022 走看看