WSO2 XML Declaration

限于喜欢 提交于 2019-11-28 09:44:22

问题


I'm using WSO2 ESB 4.0.3 to deploy a simple service. I have a service returning the following XML:

<Employees>
    <Employee>
        <EmployeeID>JOHNDOE1</EmployeeID>
        <FirstName>JOHN</FirstName>
        <LastName>DOE</LastName>
    </Employee>
    <Status>1</Status>
</Employees>

The problem I'm having is that there is no XML declaration. Is there a setting that will return the response with the XML declaration included, or do I need to use the ESB response to add it? I was hoping for something like:

<?xml version="1.0" encoding="utf-8"?>
<Employees>
    <Employee>
        <EmployeeID>JOHNDOE1</EmployeeID>
        <FirstName>JOHN</FirstName>
        <LastName>DOE</LastName>
    </Employee>
    <Status>1</Status>
</Employees>

Any help is appreciated.


回答1:


This is an old question, but seeing as I ran into the same thing just now I'll post my solution.

I needed to have a proxy service return a plain XML message without the enclosing soap envelope. I tried using application/xml and text/xml (org.apache.axis2.transport.http.ApplicationXMLFormatter and org.wso2.carbon.relay.ExpandingMessageFormatter respectively) content types to no avail. Neither of these content types returned the message with the XML declaration.

The solution is to write a custom message formatter. Here's my implementation that behaves like org.apache.axis2.transport.http.ApplicationXMLFormatter but properly writes the XML declaration to the message.

package com.example.axis2.messageformatter;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.axiom.om.OMOutputFormat;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.ApplicationXMLFormatter;

public class CustomApplicationXmlFormatter extends ApplicationXMLFormatter {

  @Override
  public void writeTo(MessageContext context, OMOutputFormat format, OutputStream out, boolean preserve) throws AxisFault {
    String xmlHeader = "<?xml version=\"1.0\" encoding=\"" + format.getCharSetEncoding() + "\"?>";
    try {
      out.write(xmlHeader.getBytes());
    } catch (IOException e) {
      throw new AxisFault("Unable to write XML declaration to output stream.", e);
    }
    super.writeTo(context, format, out, preserve);
  }
}

You can drop the class in a jar file to <ESB_ROOT>/repository/components/lib. Additionally you need to refer to the class from the axis2 config (<ESB_ROOT>/repository/conf/axis2/axis2.xml) by adding the following into the message formatters portion of the file:

<messageFormatter contentType="application/xml" class="com.example.axis2.messageformatter.CustomApplicationXmlFormatter"/>



回答2:


As Kallja answered before - messageFormatter should be fixed.

I've implemented his fix as a maven-based project - available here: https://github.com/akakunin/custom-appxml-message-formatter

I've tested it with WSO2 EI 6.0.0 - it works fine for me.




回答3:


How you captured SOAP message? You can use Tcpmon or just add a log mediator with log level full to observe the complete the complete message. I assume you what you have observed is SOAP body. You do not need to add xml declaration etc. manually.

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">
  <m:GetStockPriceResponse>
    <m:Price>34.5</m:Price>
  </m:GetStockPriceResponse>
</soap:Body>

</soap:Envelope>


来源:https://stackoverflow.com/questions/13019908/wso2-xml-declaration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!