简体   繁体   English

如何在 spring 引导中序列化和反序列化 XML?

[英]How to serialize and deserialize the XML in spring boot?

I have an XML request and the response of media type TEXT/XML.我有一个 XML 请求和媒体类型 TEXT/XML 的响应。

I am trying to serialize and deserialize the request and response in spring boot.我正在尝试序列化和反序列化 spring 引导中的请求和响应。

Request body as follows.请求正文如下。

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ws="http://ws">
   <soapenv:Header />
   <soapenv:Body>
      <ws:doKF>
         <parameterIn><![CDATA[<KFWS>
  <RqUID>0f73a59f-8388-492e-b17d-ba1fb1b1d2fd</RqUID>
  <ClientApp>A</ClientApp>
  <SignOn>
    <Username>web</Username>
    <Password>Web@123</Password>
  </SignOn>
  <CaListOfflineRq>
    <CR>123456</CR>
  </CaListOfflineRq>
</KFWS>]]></parameterIn>
      </ws:doKF>
   </soapenv:Body>
</soapenv:Envelope>

The response as follows回复如下

            <?xml version="1.0" encoding="UTF-8"?>
        <NS1:Envelope xmlns:NS1="http://schemas.xmlsoap.org/soap/envelope/">
           <NS1:Body>
              <NS2:doKFResponse xmlns:NS2="http://ws">
                 <NS2:parameterOut>
                    <KFWS>
                       <RqUID>0f73a59f-8388-492e-b17d-ba1fb1b1d2fd</RqUID>
                       <LogID>48151382-77bc-11eb-8dbc-0a05010a0000</LogID>
                       <Status>
                          <StatusCode>0</StatusCode>
                          <StatusDesc>Success</StatusDesc>
                       </Status>
                       <CaListOfflineRs>
                          <ReturnCode>0</ReturnCode>
                          <ReturnDesc>Success</ReturnDesc>
                          <CaCards>
                             <CaCard>
                                <CaMasked>490061</CaMasked>
                                <CaNoToken>2140</CaNoToken>
                                <CaToken>2140</CaToken>
                                <Name>xxxxxxxxxxxxxxx</Name>
                                <Type>Primary</Type>
                                <Class>SMART</Class>
                                <Status>Expired</Status>
                                <Number>20000</Number>
                             </CaCard>
                             <CaCard>
                                <CaMasked>4904061</CaMasked>
                                <CaNoToken>2140</CaNoToken>
                                <CaToken>2140</CaToken>
                                <Name>xxxxxxxxxxxxxxx</Name>
                                <Type>Primary</Type>
                                <Class>SMART</Class>
                                <Status>Expired</Status>
                                <Number>20000</Number>
                             </CaCard>
                          </CaCards>
                       </CaListOfflineRs>
                    </KFWS>
                 </NS2:parameterOut>
              </NS2:doKFResponse>
           </NS1:Body>
        </NS1:Envelope>

I got the response using the Feign client in the string and I need to de-serialise the response to the JSON我在字符串中使用 Feign 客户端得到响应,我需要反序列化对 JSON 的响应

Looks like it's SOAP response.看起来像是 SOAP 响应。

See Spring tutorial that shows how to consume such service.请参阅Spring 教程,该教程展示了如何使用此类服务。

You can use Jackson to serialize and deserialize your xml.您可以使用 Jackson 序列化和反序列化您的 xml。 You need to look at your xml structure and create the proper POJOs.您需要查看您的 xml 结构并创建正确的 POJO。 Here you can find how to do the serialization/deserialization https://www.baeldung.com/jackson-xml-serialization-and-deserialization在这里您可以找到如何进行序列化/反序列化https://www.baeldung.com/jackson-xml-serialization-and-deserialization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM