【问题标题】:JAX-WS always sends MTOM attachments inlineJAX-WS 总是内联发送 MTOM 附件
【发布时间】:2011-11-04 08:51:39
【问题描述】:

基本上,我想创建一个 Web 服务客户端以通过代理方法发送 mtom soap 消息。我已经从 Web 服务 wsdl 创建了我的服务工件。该消息已正确创建,但是当我启用 mtom 并添加附件时,附件始终是内联发送的,而不是在单独的 mime 部分中发送。它类似于 mtom 已启用,但由于某种原因,它决定不优化消息,因此内联发送。通过 soapui 运行相同的代码会得到正确的结果,所以我知道服务本身会接受它。

这是我创建和发送soap 请求的基本代码。 我启用了 mtomfeature,但也尝试过使用 soapBinding.setMTOMEnabled(true); 对于这两种方法,我都使用((SOAPBinding) binding).isMTOMEnabled() 对其进行了调试,以检查它是否设置为启用。

// initiate services....

// create service and enable mtom
WebServiceBlah service = new WebServiceBlah(new URL(wsdlURL), SERVICE_NAME);
WebServiceBlahPort port = service.getWebServiceBlahPort(new MTOMFeature(true, 3072));

// load file
File file = new File("/home/mypdf.pdf");
FileInputStream fileinputstream = new FileInputStream(file);
int numberBytes = fileinputstream.available();
byte bytearray[] = new byte[numberBytes];
fileinputstream.read(bytearray);
fileinputstream.close();

// create uploadResult
UploadResult request = new UploadResult();

// create attachment
AttachmentType attachment = new AttachmentType();
attachment.setContentType("application/doc");
attachment.setValue(bytearray);

// create result and add attachment to it
RenderedResult result = new RenderedResult();
result.setResult(attachment);
result.setResultContentType("pdf");
result.setResultName("a pdf file");

// add result to request
request.getResult().add(result);

// send request
port.UploadResults(request);

我得到的是我的附件是内联发送的,如下所示。 (用wireshark捕获)

POST /blah/ws/ HTTP/1.1
Content-type: multipart/related;start="<rootpart*15c3ee3b-60c7-4726-a52c-8080965e4536@example.jaxws.sun.com>";type="application/xop+xml";boundary="uuid:15c3ee3b-60c7-4726-a52c-8080965e4536";start-info="text/xml"
Soapaction: ""
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
User-Agent: JAX-WS RI 2.1.6 in JDK 6
Host: 123.123.123.123
Connection: keep-alive
Content-Length: 12372

--uuid:15c3ee3b-60c7-4726-a52c-8080965e4536    
Content-Id: <rootpart*15c3ee3b-60c7-4726-a52c-8080965e4536@example.jaxws.sun.com>    
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"    
Content-Transfer-Encoding: binary

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header></S:Header>
<S:Body>
<ns2:uploadResult xmlns:xmime="http://www.w3.org/2005/05/xmlmime">
    <renderedResult>
        <result xmime:contentType="application/doc">JVBERi0xLjQKJaqrrK0KNCAwIG9iago8</result>
        <resultContentType>pdf</resultContentType>
        <resultName>a pdf file</resultName>
    </renderedResult>
</ns2:uploadResult>
</S:Body>
</S:Envelope>
--uuid:15c3ee3b-60c7-4726-a52c-8080965e4536

我想要的是将结果标签中的附件替换为内联标签,并将附件添加到不同的 mime 部分中的肥皂消息中。例如

<result xmime:contentType='application/doc'>
    <inc:Include href="cid:myid3" xmlns:inc='http://www.w3.org/2004/08/xop/include'/>
</result>

然后将以下内容添加到soap消息中

------=_Part_10_28027205.1314348995670
Content-Type: application/pdf
Content-Transfer-Encoding: binary
Content-ID: cid:myid3
Content-Disposition: attachment; name="mypdf.pdf"
JVBERi0xLjQKJaqrrK0KNCAwIG9iago8

【问题讨论】:

  • 你解决了吗?我有完全一样的问题。 JAX-WS 文档也这样说:`正如 JAXB 2.0 规范定义的那样,xs:base64Binary 和 xs:hexBinary 映射到 java 是 byte[]。 JAX-WS 实现设置了 1KB 字节[] 大小的阈值。可以使用客户端的 RequestContext 和服务器端的 MessageContext 中的实现特定属性 com.sun.xml.ws.developer.JAXWSProperties.MTOM_THRESHOLD_VALUE 修改此阈值。您在上面的代码中将阈值设置为 3072。将此设置为 0,1,... 似乎没有什么区别。

标签: java web-services soap client mtom


【解决方案1】:

许多因素会影响 MTOM 附件是否实际使用。

在服务器上,首先是显而易见的:检查您的服务实现是否具有@MTOM 注释。您还可以使用 threshold() 属性从此注释调整阈值(正如 SteveJ 已经提到的)。

有时服务器上的处理程序会干扰是否使用 MTOM 附件。任何处理程序将 SOAP 消息序列化为字符串或字节数组(通常用于将消息内容写入日志的调试样式处理程序)将阻止使用 MTOM 附件。如果可能,请尝试禁用您的处理程序链,然后查看 MTOM 附件是否通过。

【讨论】:

  • 我知道已经很久了,但是你有什么线索可以解释为什么会这样吗?
【解决方案2】:

我遇到了同样的问题(内联附件),但监视 SOAP 消息(通过 MessageHandler)似乎是错误的:它在MtomCodec.encode(Packet packet, OutputStream out) 之前(在客户端)和之后(在服务器)播放。真实消息可以在OutputStream out的java代码中看到:

com\sun\xml\ws\jaxws-rt\2.2.10\jaxws-rt-2.2.10-sources.jar!\com\sun\xml\ws\encoding\MtomCodec.java

         for(ByteArrayBuffer bos : mtomAttachments){
             bos.write(out);
         }

仅适用于客户端的new MTOMFeature(true, 3072)

【讨论】:

    猜你喜欢
    • 2011-05-15
    • 1970-01-01
    • 2010-11-09
    • 2018-04-21
    • 2020-03-07
    • 2011-08-31
    • 2014-03-01
    • 2013-03-25
    • 2016-11-07
    相关资源
    最近更新 更多