xml反序列化错误:SAXParseException: Content is not allowed in prolog.

发布于 2022-09-11 22:15:16 字数 5372 浏览 22 评论 0

我用feign api调用远程接口并接收响应
entity结构大致为

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "DOCUMENT")
    @CompileStatic
    class CheckAccountResult {

        @XmlElement(name = "TRANSACTION_ID")
        String transactionId

        @XmlElement(name = "PARTNER_ID")
        String partnerId

        @XmlElement(name = "LOCAL_DATETIME")
        String localDateTime

        @XmlElement(name = "ACC_ID")
        String accId

        /**
         * account holder name /yes or no
         */
        @XmlElement(name = "ACC_NAME")
        String accName

        @XmlElement(name = "TRANSACTION_STATUS")
        String status

        @XmlElement(name = "ERROR_DESCRIPTION")
        String errorDescription
    }

接收远程接口响应是正常的

现在需要mock响应

    from flask import *
    app = Flask(__name__)

    @app.before_request
    def before_request():
        print(request.path)
        data = str(request.data, encoding="utf-8")
        print(data)

    @app.route('/bank-api/v1/fundtranfer',methods=['POST'])
    def fundtranfer():
        data = str(request.data, encoding="utf-8")
        print(data)
        if "070009310841" in data :
            resp_data = '<DOCUMENT><TRANSACTION_ID>20190902152540505</TRANSACTION_ID><PARTNER_ID>ATOME</PARTNER_ID><LOCAL_DATETIME>02/09/2019 15:26:08</LOCAL_DATETIME><STANID>ATOME20190902152540505</STANID><TRANSACTION_STATUS>1</TRANSACTION_STATUS><ERROR_DESCRIPTION>115 - Account ID or Account Name incorrect</ERROR_DESCRIPTION></DOCUMENT>'
            return jsonify(resp_data)

        else:
            print("未匹配到mock")
            return "success"

    @app.route('/bank-api/v1/checktransaction',methods=['POST'])
    def checktransaction():
        data = str(request.data, encoding="utf-8")
        print(data)
        resp_data = '<DOCUMENT><TRANSACTION_ID>20190906190052079</TRANSACTION_ID><PARTNER_ID>ATOME</PARTNER_ID><LOCAL_DATETIME>06/09/2019 19:01:19</LOCAL_DATETIME><TRAN_INFO>20190906170214279</TRAN_INFO><STANID>ATOME20190906170214279</STANID><TRANSACTION_STATUS>0</TRANSACTION_STATUS><ERROR_DESCRIPTION></ERROR_DESCRIPTION></DOCUMENT>'
        return jsonify(resp_data)



    @app.after_request
    def add_header(response):
        response.headers['Content-Type'] = 'application/xml;charset=UTF-8'
        response.headers['content-language'] = 'en-US'
        return response

    if __name__ =='__main__':
        app.run(host="0.0.0.0",
                port=12346,
                debug=True
                )
             

调用这个mock接口却总是报错,如何处理啊?

    Caused by: org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to [class ai.advance.payment.dto.sacombank.SacombankCheckResult]: null; nested exception is javax.xml.bind.UnmarshalException
     - with linked exception:
    [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]
        at org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter.readFromSource(Jaxb2RootElementHttpMessageConverter.java:148)
        at org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.readInternal(AbstractXmlHttpMessageConverter.java:61)
        at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:193)
        at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:105)
        at org.springframework.cloud.netflix.feign.support.SpringDecoder.decode(SpringDecoder.java:59)
        at org.springframework.cloud.netflix.feign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:37)
        at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:165)
        ... 63 common frames omitted
    Caused by: javax.xml.bind.UnmarshalException: null
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:140)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:123)
        at org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter.readFromSource(Jaxb2RootElementHttpMessageConverter.java:133)
        ... 69 common frames omitted
    Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLError

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2022-09-18 22:15:16

在你的所有 XML 前放上声明<?xml version="1.0" encoding="UTF-8"?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文