Apache CXF Spring SOAP MTOM example
来源:互联网 发布:解放军报网络图片赵阳 编辑:程序博客网 时间:2024/04/20 11:16
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.activation.DataHandler;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlMimeType;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "Book")@XmlAccessorType(XmlAccessType.FIELD)public class Book {private int bookId;private String bookISBNnumber;private String bookName;private double price;private String author;@XmlMimeType("application/octet-stream")private DataHandler image;public DataHandler getImage() {return image;}public void setImage(DataHandler image) {this.image = image;}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;@WebServicepublic interface IBookService {@WebMethodpublic void addBook(Book book)throws Exception ;@WebMethodpublic void deleteBook(int bookId);@WebMethodpublic void updateBook(Book book);@WebMethodpublic Book getBook(int bookId);}BookService.java
package com.unei.service.impl;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStream;import javax.activation.DataHandler;import com.unei.bean.Book;import com.unei.service.IBookService;public class BookService implements IBookService {@Overridepublic void addBook(Book book) throws Exception {System.out.println("addBook:" + book.getBookName());DataHandler dh = book.getImage();if (dh != null) {String fileName = dh.getName();FileOutputStream out = new FileOutputStream(new File("E:"+File.separator+fileName));byte[] b = new byte[1];InputStream ins = dh.getInputStream();while (ins.read(b) != -1) {out.write(b);}out.flush();out.close();ins.close();}else{System.out.println("image is null");}}@Overridepublic void deleteBook(int bookId) {System.out.println("deleteBook:" + bookId);}@Overridepublic void updateBook(Book book) {System.out.println("updateBook:" + book.getBookName());}@Overridepublic 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
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.4"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <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.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="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:properties><entry key="mtom-enabled" value="true"/></jaxws:properties><jaxws:serviceBean><ref bean="bsServiceBean" /></jaxws:serviceBean><jaxws:inInterceptors><ref bean="inLog" /></jaxws:inInterceptors><jaxws:outInterceptors><ref bean="outLog"/></jaxws:outInterceptors></jaxws:server></beans>注意添加:
xmlns:jaxws="http://cxf.apache.org/jaxws"
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
<jaxws:..>标签中添加
<jaxws:properties>
<entry key="mtom-enabled" value="true"/>
</jaxws:properties>
JAX-WS默认禁用MTOM功能,客户端和服务器都要添加。
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.客户端,项目结构如下:
7. 代码
Client.java
package com.unei.app;import java.io.File;import javax.activation.DataHandler;import javax.activation.FileDataSource;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) throws Exception {ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");IBookService client=(IBookService)context.getBean("client");Book b=new Book();b.setBookId(1);b.setBookName("new book");DataHandler dh=new DataHandler(new FileDataSource(new File("D:"+File.separator+"soap.png")));if(dh!=null)b.setImage(dh);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>--><jaxws:client id="client" serviceClass="com.unei.soap.client.IBookService" address="http://localhost:8080/soap/services/bs"><jaxws:properties><entry key="mtom-enabled" value="true"/></jaxws:properties></jaxws:client></beans>
8. 程序测试,运行客户端
- Apache CXF Spring SOAP MTOM example
- Apache CXF Spring SOAP example
- Apache CXF Spring SOAP Handler example
- Apache CXF REST example
- Apache CXF REST example
- Apache CXF REST example
- Apache CXF JAX-WS example
- cxf 中的 MTOM
- CXF+Spring 发布SOAP服务
- Apache CXF与Spring集成实现Soap Webservice与RESTFul WebService
- apache cxf-2.4.3 +spring-3.0.5发布SOAP协议WebService服务
- WebService CXF --- 传输文件MTOM
- apache cxf ws spring
- Spring整合Apache CXF
- Spring+Apache CXF集成
- Developing SOAP Web service using Apache CXF
- 使用CXF和Spring发布Soap服务
- Web Service ---- CXF + SPRING 开发 SOAP服务
- CERC2012 D - Non-boring sequences 暴力
- BLE芯片CC254x 系统资源(1)
- memcached安装
- poj 1656 Counting Black
- linux启动流程图+mysql协议图
- Apache CXF Spring SOAP MTOM example
- 动态规划入门——Piggy-Bank
- Java对MySQL数据库进行连接、查询和修改
- memcache文章汇总
- 链接推荐整理
- java直接rs.updateRow()来更新数据库
- POJ 2411 Mondriaan's Dream
- hdu1074 状压dp
- 分享:让office2007所有版本激活并通过微软正版验证的破解补丁