问题描述
我正在使用 WSO2 ESB 4.0.3 来部署一个简单的服务.我有一个返回以下 XML 的服务:
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>
我遇到的问题是没有 XML 声明.是否有设置会返回包含 XML 声明的响应,或者我是否需要使用 ESB 响应来添加它?我希望有类似的东西:
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>
感谢任何帮助.
推荐答案
这是一个老问题,但看到我刚才遇到同样的问题,我会发布我的解决方案.
This is an old question, but seeing as I ran into the same thing just now I'll post my solution.
我需要一个代理服务返回一个没有封闭的soap信封的纯XML消息.我尝试使用 application/xml
和 text/xml
(org.apache.axis2.transport.http.ApplicationXMLFormatter
和 org.wso2.carbon.relay.ExpandingMessageFormatter
)内容类型无济于事.这些内容类型都没有返回带有 XML 声明的消息.
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.
解决方案是编写自定义消息格式化程序.这是我的实现,其行为类似于 org.apache.axis2.transport.http.ApplicationXMLFormatter
但正确地将 XML 声明写入消息.
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);
}
}
您可以将 jar 文件中的类放到 /repository/components/lib
.此外,您需要通过将以下内容添加到文件的消息格式化程序部分来引用 axis2 配置 (<ESB_ROOT>/repository/conf/axis2/axis2.xml
) 中的类:>
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"/>
这篇关于WSO2 XML 声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!