MTOM是一个标准的服务,让你的服务传输二进制数据更方便,更有效率。很多框架都支持MTOM---Axis2,JAX-WS RI, JBoss WS , XFire , Microsoft WCF ,等更多.如果二进制是xml文档的一部分,它需要base64编码。当MTOM在一个服务上启用时,它可能需要二进制数据且通常是xml文档的一部分,并为它创建一个附件。
启用MTOM是一个相当简单的过程。首先,你必须注解POJO类型或schema类型.........下面告诉你怎样为JAXB激活MTOM. MTOM也支持Aegis.
1. 注解
a. 为MTOM修改你的schema
假如我们有这样一个Picture的schema类型,如:
<schema targetNamespace="http://pictures.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <element name="Picture"> <complexType> <sequence> <element name="Title" type="xsd:string"/> <element name="ImageData" type="xsd:base64Binary"/> </sequence> </complexType> </element> </schema>
在这种情况下,ImageData元素是我们想作为附件传送。我们只需要添加一个xmime : expectedContentTypes 注解:
<schema targetNamespace="http://pictures.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xmime="http://www.w3.org/2005/05/xmlmime"> <element name="Picture"> <complexType> <sequence> <element name="Title" type="xsd:string"/> <element name="ImageData" type="xsd:base64Binary" xmime:expectedContentTypes="application/octet-stream"/> </sequence> </complexType> </element> </schema>
这告诉JAXB(其中WSDL2Java用来为你的服务生成POJO),这个字段可以是任何内容类型。 而不是为base64Binary创建一个byte[]数组. 它将创建一个DataHandler ,而不是用于流数据。
b. 注解你的JAXB bean来启用MTOM
@XmlType public class Picture { private String title; @XmlMimeType("application/octet-stream") private DataHandler imageData; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public DataHandler getImageData() { return imageData; } public void setImageData(DataHandler imageData) { this.imageData = imageData; } }
2. 在你的服务上启用MTOM
如果你使用JAX-WS API发布你的服务,你可以像这样启用MTOM,如:
import javax.xml.ws.Endpoint; import javax.xml.ws.soap.SOAPBinding; Endpoint ep = Endpoint.publish("http://localhost/myService", new MyService()); SOAPBinding binding = (SOAPBinding) ep.getBinding(); binding.setMTOMEnabled(true);
或者,你用xml来发布你的服务:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> <jaxws:endpoint id="helloWorld" implementor="demo.spring.HelloWorldImpl" address="http://localhost/HelloWorld"> <jaxws:properties> <entry key="mtom-enabled" value="true"/> </jaxws:properties> </jaxws:endpoint> </beans>
如果你使用simple frontend,你可以在ServerFactoryBean或ClientProxyFactoryBean设置mtom-enabled属性.
Map<String,Object> props = new HashMap<String, Object>(); props.put("mtom-enabled", Boolean.TRUE); // Boolean.TRUE or "true" will work as the property value here ClientProxyFactoryBean pf = new ClientProxyFactoryBean(); pf.setPropertyies(props); .... YourClient client = (YourClient) pf.create(); ServerFactoryBean sf = new ServerFactoryBean(); sf.setPropertyies(props); ... sf.create();
你也可以用xml来配置(simple frontend):
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:simple="http://cxf.apache.org/simple" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/simple http://cxf.apache.org/schema/simple.xsd"> <simple:server id="helloWorld" serviceClass="demo.spring.HelloWorldImpl" address="http://localhost/HelloWorld"> <simple:properties> <entry key="mtom-enabled" value="true"/> </simple:properties> </simple:server> <simple:client id="helloWorldClient" serviceClass="demo.spring.HelloWorldImpl" address="http://localhost/HelloWorld"> <simple:properties> <entry key="mtom-enabled" value="true"/> </simple:properties> </simple:client> </beans>
3. 使用DataHandlers.
Picture picture = ...; DataHandler handler = picture.getImageData(); InputStream is = handler.getInputStream();
DataSource source = new ByteArrayDataSource(new byte[] {...}, "content/type"); DataSource source = new FileDataSource(new File("my/file")); Picture picture = new Picture(); picture.setImageData(new DataHandler(source));