Apache CXF Spring SOAP example

  1. 环境说明

jdk 1.6.0-29

apache cxf 2.7.7

2. 新建web project,并添加apache cxf/lib目录下所需jar,软件目录如下:

3. 程序代码

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

}

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	version="2.4">
	<!-- 指明spring配置文件在何处 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext.xml</param-value>
	</context-param>
	<!-- 加载spring配置文件applicationContext.xml -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<servlet>
		<servlet-name>cxf</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>cxf</servlet-name>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>

</web-app>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd
    ">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<aop:aspectj-autoproxy proxy-target-class="true" />
	<bean id="bsServiceBean" class="com.unei.service.impl.BookService" />
	<bean id="inLog" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
	<bean id="outLog" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>

	<jaxws:server id="bookService" serviceClass="com.unei.service.IBookService" address="/bs">
		<jaxws:serviceBean>
			<ref bean="bsServiceBean" />
		</jaxws:serviceBean>

		<jaxws:inInterceptors>
			<ref bean="inLog" />
		</jaxws:inInterceptors>
		
		<jaxws:outInterceptors>
			<ref bean="outLog"/>
		</jaxws:outInterceptors>
	</jaxws:server>
</beans>

4.  程序测试

浏览器输入:http://localhost:8080/soap/services/bs?wsdl 

浏览器输出wsdl文档

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

在命令提示符中运行:

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

6. 客户端编写,新建web project

将上述生成的代码添加进项目中,项目结构如下:

7. 项目代码

Client.java

package com.unei.app;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.unei.soap.client.Book;
import com.unei.soap.client.IBookService;


public class Client {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		IBookService client=(IBookService)context.getBean("client");
		Book b=new Book();
		b.setBookId(1);
		b.setBookName("new book");
		client.addBook(b);
	}

}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd
    ">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<aop:aspectj-autoproxy proxy-target-class="true" />
	
	<bean id="client" class="com.unei.soap.client.IBookService" factory-bean="clientFactory"
		factory-method="create"/>
	
	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass" value="com.unei.soap.client.IBookService"/>
		<property name="address" value="http://localhost:8080/soap/services/bs"/>
	</bean>
</beans>

8. 运行客户端,查看服务器输出信息

2013-10-3 17:45:59 org.apache.cxf.services.IBookServiceService.IBookServicePort.IBookService
信息: Inbound Message
----------------------------
ID: 2
Address: http://localhost:8080/soap/services/bs
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], cache-control=[no-cache], connection=[keep-alive], Content-Length=[251], content-type=[text/xml; charset=UTF-8], host=[localhost:8080], pragma=[no-cache], SOAPAction=[""], user-agent=[Apache CXF 2.7.7]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:addBook xmlns:ns2="http://service.unei.com/"><arg0><bookId>1</bookId><bookName>new book</bookName><price>0.0</price></arg0></ns2:addBook></soap:Body></soap:Envelope>
--------------------------------------
addBook:new book
2013-10-3 17:46:00 org.apache.cxf.services.IBookServiceService.IBookServicePort.IBookService
信息: Outbound Message
---------------------------
ID: 2
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:addBookResponse xmlns:ns2="http://service.unei.com/"/></soap:Body></soap:Envelope>
--------------------------------------


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