如何格式化Spring Boot返回的JSON结果

时间:2019-09-04 23:51:02

标签: json api

我的spring boot程序(http://localhost:8080/api/cutomers)将json文件返回为:

[
    {
        id: 1,
        address: "Super Mart of the West",
        state: "Arkansas",
        companyName: "Bentonville",
        zipcode: "72716",
        city: "702 SW 8th Street",
        phone: "(800) 555-2171",
        fax: "(800) 555-2797"
    },
    {
        id: 2,
        address: "Electronics Depot",
        state: "Georgia",
        companyName: "Atlanta",
        zipcode: "30339",
        city: "2455 Paces Ferry Road NW",
        phone: "(800) 595-3231",
        fax: "(800) 595-3232"
    },{
        id: 3, 
        address: "Screen Shop",
        state: "North Carolina",
        companyName: "Mooresville",
        zipcode: "28117",
        city: "1000 Lowes Blvd",
        phone: "(800) 445-6938",
        fax: "(800) 445-6937"
    }
]

要将这些数据公开到devextreme gx-grid中,我想使用这种json格式:

{
    totalCount: 3,
    items: [
        {
            id: 1,
            address: "Super Mart of the West",
            state: "Arkansas",
            companyName: "Bentonville",
            zipcode: "72716",
            city: "702 SW 8th Street",
            phone: "(800) 555-2171",
            fax: "(800) 555-2797"
        },
        {
            id: 2,
            address: "Electronics Depot",
            state: "Georgia",
            companyName: "Atlanta",
            zipcode: "30339",
            city: "2455 Paces Ferry Road NW",
            phone: "(800) 595-3231",
            fax: "(800) 595-3232"
        },{
            id: 3,
            address: "Screen Shop",
            state: "North Carolina",
            companyName: "Mooresville",
            zipcode: "28117",
            city: "1000 Lowes Blvd",
            phone: "(800) 445-6938",
            fax: "(800) 445-6937"
        }
    ]
}

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以创建一个包含项目数组和计数属性的父DTO。  返回父DTO作为响应。 Spring Rest控制器将负责以这种格式显示