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"/>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…