Soap Response XML到Json-春季启动

时间:2019-06-05 23:27:08

标签: json xml spring-boot soap

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

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

来自WSDL的肥皂响应

<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%"
        }
    }
]
}

1 个答案:

答案 0 :(得分:0)

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"
        //   }
        // }
    }
}