You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为SOAP请求添加image/jpeg格式附件?基于现有方法实现

问题描述

我已有一个可正常运行的SOAP请求发送方法:

public <T, L> L sendRequest(T request) {

        L responseClass;
        try {
            responseClass = (L) getWebServiceTemplate()
                    .marshalSendAndReceive(uri, request,
                                           new SoapActionCallback(
                                                   uri + "/" + request.getClass().getName()));
        } catch (SoapFaultClientException ex) {
            throw new CommonException(HttpStatus.BAD_REQUEST, "Some error", ex);
        } catch (Exception ex) {
            throw new CommonException(HttpStatus.BAD_REQUEST, "Some error 2", ex);
        }

        return responseClass;
    }

请求对象由WSDL生成,现在需要实现一个支持发送image/jpeg格式附件的SOAP请求方法:public <T, L> L sendRequestWithAttachment(T request, byte[] imageData),了解大概需要用到AttachmentPart,但不清楚具体实现方式,求帮助。

解决方案

基于你现有的WebServiceTemplate实现,我们可以通过自定义WebServiceMessageCallback来添加SOAP附件,具体代码如下:

public <T, L> L sendRequestWithAttachment(T request, byte[] imageData) {
    L responseClass;
    try {
        responseClass = (L) getWebServiceTemplate().marshalSendAndReceive(uri, request,
                new WebServiceMessageCallback() {
                    @Override
                    public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
                        // 转换为SAAJ SOAP消息,以便操作附件
                        SaajSoapMessage saajMessage = (SaajSoapMessage) message;
                        SOAPMessage soapMessage = saajMessage.getSaajMessage();

                        try {
                            // 创建附件部分
                            AttachmentPart attachmentPart = soapMessage.createAttachmentPart();
                            // 设置附件内容:字节数组、MIME类型、附件ID
                            attachmentPart.setContent(imageData, "image/jpeg");
                            attachmentPart.setContentId("<image-attachment>"); // 自定义ID,需和请求中的关联字段匹配(如果WSDL有定义)

                            // 将附件添加到SOAP消息
                            soapMessage.addAttachmentPart(attachmentPart);

                            // 设置SOAP Action,和原方法逻辑保持一致
                            String soapAction = uri + "/" + request.getClass().getName();
                            saajMessage.setSoapAction(soapAction);
                        } catch (SOAPException e) {
                            throw new IOException("Failed to create SOAP attachment", e);
                        }
                    }
                });
    } catch (SoapFaultClientException ex) {
        throw new CommonException(HttpStatus.BAD_REQUEST, "Some error", ex);
    } catch (Exception ex) {
        throw new CommonException(HttpStatus.BAD_REQUEST, "Some error 2", ex);
    }

    return responseClass;
}

关键注意点

  • SAAJ消息转换:必须把WebServiceMessage转为SaajSoapMessage,这是Spring封装的JAX-WS SAAJ API实现,只有这样才能直接操作SOAP附件。
  • 附件ID匹配:setContentId的取值要和WSDL定义的附件引用规则一致。如果生成的请求对象里有对应关联字段(比如attachmentRefId),调用方法前要把该字段设为附件的ContentId(不带尖括号,比如request.setAttachmentRefId("image-attachment")),否则服务端无法关联附件和请求内容。
  • 异常处理:保持和原方法相同的异常捕获逻辑,把SOAP相关异常转为IOException后统一抛出,确保异常处理逻辑一致。
  • SOAP Action对齐:在回调里手动设置SOAP Action,和原方法的生成逻辑保持一致,保证服务端能正确识别请求对应的操作。

内容的提问来源于stack exchange,提问作者SNap97

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 22:46:29