1. 环境说明
jdk 1.6.0_29
apache cxf 2.7.7
2. 新建Java Project
3. 添加jar包,将apache cxf下面lib里面的jar包都添加到项目中(可能有些是不需要的,为了简单没进行筛选)。
4. 项目目录结构:
5. 代码如下
Book.java
- package com.unei.bean;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement(name = "Book")
- public class Book {
- private int bookId;
- private String bookISBNnumber;
- private String bookName;
- private double price;
- private String author;
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- public int getBookId() {
- return bookId;
- }
- public void setBookId(int bookId) {
- this.bookId = bookId;
- }
- public String getBookISBNnumber() {
- return bookISBNnumber;
- }
- public void setBookISBNnumber(String bookISBNnumber) {
- this.bookISBNnumber = bookISBNnumber;
- }
- public String getBookName() {
- return bookName;
- }
- public void setBookName(String bookName) {
- this.bookName = bookName;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- }
IBookService.java
- package com.unei.service;
- import javax.jws.WebMethod;
- import javax.jws.WebService;
- import com.unei.bean.Book;
- @WebService
- public interface IBookService {
- @WebMethod
- public void addBook(Book book);
- @WebMethod
- public void deleteBook(int bookId);
- @WebMethod
- public void updateBook(Book book);
- @WebMethod
- public Book getBook(int bookId);
- }
BookService.java
- package com.unei.service.impl;
- import com.unei.bean.Book;
- import com.unei.service.IBookService;
- public class BookService implements IBookService{
- @Override
- public void addBook(Book book) {
- System.out.println("addBook:"+book.getBookName());
- }
- @Override
- public void deleteBook(int bookId) {
- System.out.println("deleteBook:"+bookId);
- }
- @Override
- public void updateBook(Book book) {
- System.out.println("updateBook:"+book.getBookName());
- }
- @Override
- public Book getBook(int bookId) {
- Book book=new Book();
- book.setBookId(4);
- book.setBookName("getBook");
- book.setBookISBNnumber("ABCDEFG");
- book.setPrice(20.00);
- return book;
- }
- }
Server.java
- package com.unei.service.test;
- import org.apache.cxf.interceptor.LoggingInInterceptor;
- import org.apache.cxf.interceptor.LoggingOutInterceptor;
- import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
- import com.unei.service.IBookService;
- import com.unei.service.impl.BookService;
- public class Server {
- protected Server() throws Exception {
- <span style="white-space:pre"> </span>System.out.println("Starting server...");
- <span style="white-space:pre"> </span>BookService bs=new BookService();
- <span style="white-space:pre"> </span>JaxWsServerFactoryBean svrFactory=new JaxWsServerFactoryBean();
- <span style="white-space:pre"> </span>svrFactory.setServiceClass(IBookService.class);
- <span style="white-space:pre"> </span>svrFactory.setAddress("http://localhost:9000/bs");
- <span style="white-space:pre"> </span>svrFactory.setServiceBean(bs);
- <span style="white-space:pre"> </span>svrFactory.getInInterceptors().add(new LoggingInInterceptor());
- <span style="white-space:pre"> </span>svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
- <span style="white-space:pre"> </span>svrFactory.create();
- }
- public static void main(String args[]) throws Exception {
- new Server();
- System.out.println("Server ready...");
- Thread.sleep(5 * 6000 * 1000);
- System.out.println("Server exiting");
- System.exit(0);
- }
- }
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
- package com.unei.app;
- import org.apache.cxf.interceptor.LoggingInInterceptor;
- import org.apache.cxf.interceptor.LoggingOutInterceptor;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- import com.unei.soap.client.Book;
- import com.unei.soap.client.IBookService;
- public class Client {
- public static void main(String[] args) {
- JaxWsProxyFactoryBean factory=new JaxWsProxyFactoryBean();
- factory.getInInterceptors().add(new LoggingInInterceptor());
- factory.getOutInterceptors().add(new LoggingOutInterceptor());
- factory.setAddress("http://localhost:9000/bs");
- factory.setServiceClass(IBookService.class);
- Object o=factory.create();
- IBookService bs=(IBookService)o;
- Book b=new Book();
- b.setBookName("new book");
- b.setBookId(1);
- bs.addBook(b);
- }
- }