【问题标题】:MTOM - Root content type was always text/xmlMTOM - 根内容类型始终为 text/xml
【发布时间】:2016-07-16 23:19:36
【问题描述】:

我正在使用 MTOM 将附件从客户端流式传输到服务器。

应用 MTOM,文件以二进制形式流式传输。但根 Content-Type 始终是 "text/xml",应该是 "application/xml+xop"。

问题仅出现在 websphere 中。内容类型在 websphere 中设置为 "text/xml"

websphere liberity profile中,内容类型设置为"application/xml+xop"

------=_Part_7283_-2062365125.1458743649653
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-Id: <511212039242.1458743649653.IBM.WEBSERVICES@lsrv4665.linux.rabobank.nl>

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <soapenv:Header>
        </soapenv:Header>
        <soapenv:Body>
          <Content><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:58cf03d2-322f-4819-80fb-3b001f497d12%40www.test.com"/>
          </Content>
        </soapenv:Body>
    </soapenv:Envelope>

    Content-Type: application/pdf; name=attachment.pdf

Content-Transfer-Encoding: binary

【问题讨论】:

    标签: java soap websphere websphere-liberty mtom


    【解决方案1】:

    我收集了几个答案。希望第一个答案适合您。为了预防措施,我还添加了一些其他带有链接的答案。希望它能拯救你。

    答案一:

    服务器端(Weblogic 中的 JAX-WS)

    使用@MTOM 注释或mtom.xml 策略

    客户端(Weblogic 中的 JAX-WS)

    Pass MTOMFeature() as argument:
    MtomService port = service.getMailServicePort(new MTOMFeature());
    

    通过 SOAPUI 进行 MTOM 附件,3 个步骤:

    1. Set Enable MTOM = true 在请求属性中
    2. 上传附件(如A3.pdf),注意contentID
    3. 在 xml 请求中设置 MTOM contentID

    这是一个带有 weblogic 的图像的完整示例。希望它适合您的问题。(Sending attachment: MTOM / XOP vs SWA and inline attachment 的链接)

    另一个资源链接:

    1. Steps to Use MTOM/XOP to Send Binary Data
    2. Error consuming webservice, content type “application/xop+xml” does not match expected type “text/xml”

    答案2:

    引入saaj-impl 1.3.23 并为javax.xml.soap.* 首选应用程序类解决了这个问题。

    资源链接:https://jira.spring.io/browse/SWS-855


    答案3:

    来自mkyong的tutorial,可以解决在客户端和服务器上启用mtom的问题。

    在服务器上启用 MTOM:

    启用服务器通过MTOM发送附件非常简单,只需在Web服务实现类上注解javax.xml.ws.soap.MTOM即可。

    在客户端启用 MTOM:

    允许客户端通过 MTOM 向服务器发送附件需要一些额外的工作,请参见以下示例:

    //codes enable MTOM in client
    BindingProvider bp = (BindingProvider) imageServer;
    SOAPBinding binding = (SOAPBinding) bp.getBinding();
    binding.setMTOMEnabled(true);
    

    Ans-4

    归功于@BalusC。他的精彩教程给出了一个很棒的答案。

    当页面通过 HTTP 提供时,元标记将被忽略。

    使用 JSP 时,

    你应该把&lt;%@ page pageEncoding="UTF-8" %&gt;放在最上面。

    使用 Servlet 时,

    你应该这样做response.setCharacterEncoding("UTF-8");

    两者都会在内容类型标头中隐式设置正确的字符集。您可能会发现这篇文章很有用:Unicode - How to get characters right?。对于 JSP/Servlet 解决方案,请从 this chapter 开始。

    资源链接:

    1. How to set the "Content-Type ... charset" in the request header using a HTML link

    为了研究,你可以通过以下方式

    对于 Java servlet,你应该有一行

    response.setContentType("text/html");

    doGetdoPost 方法的顶部,其中 response 是对 HttpServletResponse 的引用。

    相关链接:

    1. How to set up your server to send the correct MIME types
    2. Character Encoding problem with IBM's JSF and Ajax

    另一个答案

    我已经找出导致问题的原因,但我不明白为什么。当请求发生错误操作时,该行为会表现出来。附件是一个简单 MPG 的压缩包,其中包含一个请求、响应和错误规则来证明这一点。该请求有一个错误操作、一个执行 dp:reject(强制错误)的简单 xform 和一个结果操作。错误规则有一个 results 动作和一个 set var 动作。如果您保留 on-error,则响应内容类型将返回为“text/xml”。如果删除 on-error,则内容类型会正确返回“application/json”。 (从以下资源链接复制)

    资源链接:

    1. How to set header Content-Type in error rule

    【讨论】:

      【解决方案2】:

      能够通过使用 saaj-impl jar 解决此问题。

      pom.xml

       <dependency>
          <groupId>com.sun.xml.messaging.saaj</groupId>
          <artifactId>saaj-impl</artifactId>
          <version>1.3.16</version>
          <scope>provided</scope>
       </dependency>
      

      dispatcher-servlet.xml

      <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
        <property name="messageFactory">
          <bean class="com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl" />
        </property>
      </bean>
      

      【讨论】:

      • 谢谢 AJJ。您的解决方案对我很有帮助。
      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      相关资源
      最近更新 更多