Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
673 views
in Technique[技术] by (71.8m points)

wso2esb - WSO2 XML Declaration

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...