今天看互联互通共享文档传输需要MTOM/XOP编码,做个简单笔记
服务端代码
webservice接口
import com.aadata.qyhlht.entity.User;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService(name="MtomTest",targetNamespace = "http://www.chiss.org.cn/rhin/2015")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface MtomTest {
@WebMethod(operationName="addUser",action="addUser")
@WebResult(name = "addUserResponse",targetNamespace = "http://www.chiss.org.cn/rhin/is/2015",partName = "message")
public User addUser(@WebParam(name = "addUser",targetNamespace = "http://www.chiss.org.cn/rhin/2015",partName = "addUser")User user);
}
webservice实现类
其中@MTOM表示二进制传输使用MTOM编码
import java.io.File;
import java.io.IOException;
import com.aadata.qyhlht.entity.User;
import com.aadata.qyhlht.webservice.MtomTest;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.MTOM;
@Component
@WebService(name="MtomTest",serviceName="MtomTest"
,targetNamespace="http://www.chiss.org.cn/rhin/2015")
@BindingType(value= javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
@MTOM
public class MtomTestImpl implements MtomTest {
@Override
public User addUser(User user) {
byte[] content = user.getContent();
try {
FileUtils.writeByteArrayToFile(new File("C:/aadata/test.txt"), content);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(user);
return user;
}
}
User实体类
import lombok.Data;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class User {
private String name = "wy";
private String sex = "man";
public int age = 20;
@XmlElement(required = true)
protected byte[] content;
}
客户端是使用CXF反向生成的代码;
其中MtomTest port = ss.getMtomTestPort(new MTOMFeature());是使用Mtom编码否则不是
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
public final class MtomTest_MtomTestPort_Client {
private static final QName SERVICE_NAME = new QName("http://www.chiss.org.cn/rhin/2015", "MtomTest");
private MtomTest_MtomTestPort_Client() {
}
public static void main(String args[]) throws Exception {
URL wsdlURL = MtomTest_Service.WSDL_LOCATION;
if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
File wsdlFile = new File(args[0]);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
MtomTest_Service ss = new MtomTest_Service(wsdlURL, SERVICE_NAME);
//MTOM方式
// MtomTest port = ss.getMtomTestPort(new MTOMFeature());
MtomTest port = ss.getMtomTestPort();
System.out.println("Invoking addUser...");
byte[] uploadFile = FileUtils.readFileToByteArray(new File("C:/aadata/abc.log"));
com.aadata.User addUser = new User();
addUser.setAge(100);
addUser.setName("名称");
addUser.setContent(uploadFile);
com.aadata.User addUserreturn = port.addUser(addUser);
System.out.println("addUser.result=" + addUserreturn);
System.exit(0);
}
传输内容大小对比;其中入参和回参都是同样的内容;入参没有使用Mtom自动base64的二进制;回参使用Mtom;可以看出使用Mtom的数据更小