CXF集成MTOM传输数据

本文记录互联互通共享文档传输需用到的MTOM/XOP编码。介绍了服务端代码,包括webservice接口、实现类,其中@MTOM表示二进制传输用该编码,还有User实体类。客户端用CXF反向生成代码,通过特定语句使用Mtom编码。对比传输内容大小,发现使用Mtom的数据更小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天看互联互通共享文档传输需要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的数据更小

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值