Apache Cxf端点中的Unescape Xml特殊字符

时间:2017-04-12 14:23:07

标签: java xml apache web-services cxf

我需要在apache cxf端点中返回未转义的xml字符串。下面我粘贴我的代码的部分,返回当前的xml字符串和所需的xml字符串:

@WebService(endpointInterface = "net.system.soapservice.result.ResultMgrPortType", serviceName = "ResultMgrPortType")
public class ResultServiceImpl implements ResultMgrPortType {

@Override
public String genericAPIResult(String resultMsg) {

    Map<String, String> processedResultMsg = DataFormatUtil.parseResultMsg(resultMsg);

    String response = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            + "<soapenv:Body>"
            + "<req:ResponseMsg"
            + " xmlns:req=\"http://api-v1.gen.mm.vodafone.com/mminterface/request\"><![CDATA[<?xml"
            + " version=\"1.0\""
            + "encoding=\"UTF-8\"?><response"
            + " xmlns=\"http://api-"
            + "v1.gen.mm.vodafone.com/mminterface/response\">"
            + "<ResponseCode>"+processedResultMsg.get("ResultCode")+"</ResponseCode>"
            + "<ConversationID>"+processedResultMsg.get("ConversationID")+"</ConversationID>"
            + "<ResponseDesc>Service result processed successfully.</ResponseDesc>"
            + "<OriginatorConversationID>"+processedResultMsg.get("OriginatorConversationID")+"</OriginatorConversationID>"
            + "<ServiceStatus>0</ServiceStatus>"
            + "</response>]]></req:ResponseMsg>"
            + "</soapenv:Body>"
            + "</soapenv:Envelope>";
    return response;
}

}

来自上述端点的xml响应,如我的日志中所示:

ID: 4
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ResponseMsg xmlns="http://api-v1.gen.mm.vodafone.com/mminterface/result">&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;
&lt;soapenv:Body&gt;
&lt;req:ResponseMsg
 xmlns:req="http://api-v1.gen.mm.vodafone.com/mminterface/request"&gt;&lt;![CDATA[&lt;?xml
 version="1.0"
encoding="UTF-8"?&gt;&lt;response
 xmlns="http://api-
v1.gen.mm.vodafone.com/mminterface/response"&gt;&lt;ResponseCode&gt;0&lt;/ResponseCode&gt;&lt;ConversationID&gt;AG_20170411_000059047aba809d6631&lt;/ConversationID&gt;&lt;ResponseDesc&gt;Service result processed successfully.&lt;/ResponseDesc&gt;&lt;OriginatorConversationID&gt;902004_fhltd_60575.0&lt;/OriginatorConversationID&gt;&lt;ServiceStatus&gt;0&lt;/ServiceStatus&gt;&lt;/response&gt;]]&gt;&lt;/req:ResponseMsg&gt;
&lt;/soapenv:Body&gt;
&lt;/soapenv:Envelope&gt;</ResponseMsg></soap:Body></soap:Envelope>  
--------------------------------------

客户端预期的xml:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ResponseMsg xmlns="http://api-v1.gen.mm.vodafone.com/mminterface/result">
         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
            <soapenv:Body>
               <req:ResponseMsg xmlns:req="http://api-v1.gen.mm.vodafone.com/mminterface/request"><![CDATA[<?xml version="1.0"encoding="UTF-8"?><response xmlns="http://api-v1.gen.mm.vodafone.com/mminterface/response"><ResponseCode>0</ResponseCode><ConversationID>AG_20170406_00004ddc3d70f6ba93f0</ConversationID><ResponseDesc>Service result processed successfully.</ResponseDesc><OriginatorConversationID>902004_fhltd_94802.0</OriginatorConversationID><ServiceStatus>0</ServiceStatus></response>]]></req:ResponseMsg>
            </soapenv:Body>
         </soapenv:Envelope>
      </ResponseMsg>
   </soap:Body>
</soap:Envelope>

注意:答案不应该是&lt&gt。可能是我应该在apache-cxf-services.xml文件中添加一些配置或者创建一个拦截器来改变这种行为。提前感谢您的时间和精力。

1 个答案:

答案 0 :(得分:1)

我在这里回答我自己的问题。解决方案是创建一个拦截器来强制cxf中的jaxb来编码CDATA中的响应字符串。所以我从立即响应中删除了CDATA(现在应该由拦截器处理),结果响应应该类似于:

String response = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
            + "<soapenv:Body>"
            + "<req:ResponseMsg"
            + " xmlns:req=\"http://api-v1.gen.mm.vodafone.com/mminterface/request\"><?xml"
            + " version=\"1.0\""
            + "encoding=\"UTF-8\"?><response"
            + " xmlns=\"http://api-"
            + "v1.gen.mm.vodafone.com/mminterface/response\">"
            + "<ResponseCode>"+processedResultMsg.get("ResultCode")+"</ResponseCode>"
            + "<ConversationID>"+processedResultMsg.get("ConversationID")+"</ConversationID>"
            + "<ResponseDesc>Service result processed successfully.</ResponseDesc>"
            + "<OriginatorConversationID>"+processedResultMsg.get("OriginatorConversationID")+"</OriginatorConversationID>"
            + "<ServiceStatus>0</ServiceStatus>"
            + "</response></req:ResponseMsg>"
            + "</soapenv:Body>"
            + "</soapenv:Envelope>";

然后创建一个拦截器以在PRE_STREAM阶段添加CDATA:

import java.io.OutputStream;
import javax.xml.stream.XMLStreamWriter;
import org.apache.cxf.interceptor.AttachmentOutInterceptor;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.staxutils.StaxUtils;


public class CdataWriterInterceptor extends AbstractPhaseInterceptor<Message> {

    public CdataWriterInterceptor() {
        super(Phase.PRE_STREAM);
        addAfter(AttachmentOutInterceptor.class.getName());
    }

    @Override
    public void handleMessage(Message message) {
        System.out.println("############# CdataWriterInterceptor Executed #######################");
        message.put("disable.outputstream.optimization", Boolean.TRUE);
        XMLStreamWriter writer
                = StaxUtils.createXMLStreamWriter(message.getContent(OutputStream.class));
        message.setContent(XMLStreamWriter.class, new CDataXMLStreamWriter(writer));
    }
}

最后在CDataXMLStreamWriter中添加CDATA,如下所示:

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.apache.cxf.staxutils.DelegatingXMLStreamWriter;

public class CDataXMLStreamWriter extends DelegatingXMLStreamWriter {

    private String currentElementName;

    public CDataXMLStreamWriter(XMLStreamWriter del) {
        super(del);
    }

    @Override
    public void writeCharacters(String text) throws XMLStreamException {
            System.out.println("WritingCData" + text);
            super.writeCData(text);
    }

    private boolean checkIfCDATAneededForCurrentElement() {
        if ("MessageBody".equals(currentElementName)) {
            return true;
        }
        return false;
    }

    @Override
    public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
        currentElementName = local;
        super.writeStartElement(prefix, local, uri);
    }
}

这个想法是,如果在CDATA部分中发送xml字符串,它将不会被cxf内部xml解析器解析,因此特殊字符不会被转义。这对我有用,我希望它能为其他人工作。更多详情:CXF jaxb send string as CData