【问题标题】:Soap Response XML to Json - spring boot肥皂响应 XML 到 Json - 春季启动
【发布时间】:2019-10-21 11:34:09
【问题描述】:

我在我的项目中使用 WSDL 服务,返回的响应是 XML。对于我的应用程序,我需要以 JSON 格式传输响应。这里使用的框架是spring boot。编码语言很时髦

我已尝试将 xml 转换为 Json,但不需要响应。

来自 WSDL 的 Soap 响应

<soapenv:Envelope   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <RESP xmlns="http://xmlns.oracle.com/Enterprise/Tools/schemas/RESP">
            <Data>
                <Resp_Data>
                    <ID >123456789</ID>
                    <Name >Downey jr,Robert John</Name>
                    <LastName >Downey jr</LastName>
                    <FirstName >Robert</FirstName>
                    <MiddleName >John</MiddleName>
                    <Movies>
                        <MName>Sherlock Homes</MName>
                        <Year>2009</Year>
                        <Length>2h 8m</Length>
                        <BoxOffice>$208,711,166</BoxOffice>
                        <Ratings>
                            <ImdbRaring>7.6</ImdbRaring>
                            <RottenTomatoes>70%</RottenTomatoes>
                        </Ratings>
                    <Movies>
                    <Movies>
                        <MName>Iron Man</MName>
                        <Year>2008</Year>
                        <Length>2h 6m</Length>
                        <BoxOffice>$318,298,180</BoxOffice>
                        <Ratings>
                            <ImdbRaring>7.9</ImdbRaring>
                            <RottenTomatoes>93%</RottenTomatoes>
                        </Ratings>
                    <Movies>
                    <Movies>
                        <MName>Iron Man2</MName>
                        <Year>2008</Year>
                        <Length>2h 4m</Length>
                        <BoxOffice>$312,057,433</BoxOffice>
                        <Ratings>
                            <ImdbRaring>7.0</ImdbRaring>
                            <RottenTomatoes>73%</RottenTomatoes>
                        </Ratings>
                    <Movies>
                </Resp_Data>
            </Data>
        <RESP>
    </soapenv:Body>
</soapenv:Envelope>

我尝试使用 XML.toJSONObject(xmlString),转换后的 Json 响应是

{
    "Envelope": {
        "Body": {
            "RESP": {
                "Data": {
                    "Resp_Data": {
                        "ID": "123456789",
                        "Name": "Downey jr,Robert John",
                        "LastName": "Downey jr",
                        "FirstName": "Robert",
                        "MiddleName": "John",
                        "Movies": [
                            {
                                "MName": "Sherlock Homes",
                                "Year": "2009",
                                "Length": "2h 8m",
                                "BoxOffice": "$208,711,166",
                                "Ratings": {
                                    "ImdbRaring": "7.6",
                                    "RottenTomatoes": "70%"
                                }
                            },
                            {
                                "MName": "Iron Man",
                                "Year": "2008",
                                "Length": "2h 6m",
                                "BoxOffice": "$318,298,180",
                                "Ratings": {
                                    "ImdbRaring": "7.9",
                                    "RottenTomatoes": "93%"
                                }
                            },
                            {
                                "MName": "Iron Man2",
                                "Year": "2008",
                                "Length": "2h 4m",
                                "BoxOffice": "$312,057,433",
                                "Ratings": {
                                    "ImdbRaring": "7.0",
                                    "RottenTomatoes": "73%"
                                }
                            }
                        ]
                    }
                },
                "_xmlns": "http://xmlns.oracle.com/Enterprise/Tools/schemas/RESP"
            },
            "__prefix": "soapenv"
        },
        "_xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
        "_xmlns:soapenc": "http://schemas.xmlsoap.org/soap/encoding/",
        "_xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
        "_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "__prefix": "soapenv"
    }
}

另外,我尝试使用 groovy 的 Json Builder 来解析 XML 并创建 Json 格式。下面是代码

def responseXML = new XmlSlurper().parseText(xmlString)
                def builder = new groovy.json.JsonBuilder()
                builder {
                    responseXML.Body.RESP.Data.Resp_Data.each { nodeElem ->
                        nodeElem.childNodes().each { childElem ->
                            "$childElem.name" childElem.text()
                        }
                    }
                }

我得到的 Json 响应是

{
    "ID": "123456789",
    "Name": "Downey jr,Robert John",
    "LastName": "Downey jr",
    "FirstName": "Robert",
    "MiddleName": "John",
     "W_CHKLST_WS_REC": "Sherlock Homes 2009 2h 8m $208,711,166 7.6 70% Iron Man 2008 2h 6m $318,298,180 7.9 93% Iron Man2 2008 2h 4m $312,057,433 7.0 73%"
}

我正在尝试实现 Json 响应为

{
"ID": "123456789",
"Name": "Downey jr,Robert John",
"LastName": "Downey jr",
"FirstName": "Robert",
"MiddleName": "John",
"Movies": [
    {
        "MName": "Sherlock Homes",
        "Year": "2009",
        "Length": "2h 8m",
        "BoxOffice": "$208,711,166",
        "Ratings": {
            "ImdbRaring": "7.6",
            "RottenTomatoes": "70%"
        }
    },
    {
        "MName": "Iron Man",
        "Year": "2008",
        "Length": "2h 6m",
        "BoxOffice": "$318,298,180",
        "Ratings": {
            "ImdbRaring": "7.9",
            "RottenTomatoes": "93%"
        }
    },
    {
        "MName": "Iron Man2",
        "Year": "2008",
        "Length": "2h 4m",
        "BoxOffice": "$312,057,433",
        "Ratings": {
            "ImdbRaring": "7.0",
            "RottenTomatoes": "73%"
        }
    }
]
}

【问题讨论】:

    标签: json xml spring-boot soap


    【解决方案1】:

    Underscore-java库可以将xml转json

    import com.github.underscore.lodash.U;
    
    public class JsonConversion {
        public static String TEST_XML_STRING =
            "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
        public static void main(String args[]) {
            String jsonPrettyPrintString = U.xmlToJson(TEST_XML_STRING);
            System.out.println(jsonPrettyPrintString);
            // {
            //   "test": {
            //     "-attrib": "moretest",
            //     "#text": "Turn this to JSON"
            //   }
            // }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多