xml反序列化错误:SAXParseException: Content is not allowed in prolog.
我用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的所有 XML 前放上声明
<?xml version="1.0" encoding="UTF-8"?>