首页 > 解决方案 > 将 json 返回格式表示为不同的格式

问题描述

我在休息服务中创建了一个方法并在邮递员中对其进行了测试,它返回了这种格式:

{
    "account": [
        {
            "id": 1,
            "password": "a",
            "pop3": "pop3",
            "smtp": "smtp",
            "username": "a"
        }
    ]
}

我的服务方法代码:

@GET
@Path("/accounts")
@Produces(MediaType.APPLICATION_JSON)
public List<Account> getAccounts(){

        Account a = new Account();
        a.setId(1);
        a.setSmtp("smtp");
        a.setPop3("pop3");
        a.setUsername("a");
        a.setPassword("a");

        List<Account> accounts = new ArrayList<>();
        accounts.add(a);
        return accounts;
}

有没有办法以这种格式返回?

[
    {
        "id": 1,
        "password": "a",
        "pop3": "pop3",
        "smtp": "smtp",
        "username": "a"
    }
]

标签: javajson

解决方案


推荐阅读