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

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 {
    	System.out.println("Starting server...");
    	BookService bs=new BookService();
    	JaxWsServerFactoryBean svrFactory=new JaxWsServerFactoryBean();
    	svrFactory.setServiceClass(IBookService.class);
    	svrFactory.setAddress("http://localhost:9000/bs");
    	svrFactory.setServiceBean(bs);
    	svrFactory.getInInterceptors().add(new LoggingInInterceptor());
    	svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
    	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生成的客户端代码添加到项目中,项目如下:

Apache CXF JAX-WS example_第1张图片

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);
	}

}

你可能感兴趣的:(apache,CXF,example,jax-ws)