Returned json unexpected, has “links” spelled as “_links” and structure different, in Spring hateoas

前端 未结 6 1790
情书的邮戳
情书的邮戳 2021-01-02 02:56

As the title says, I have a resource object Product extending ResourceSupport. However, the responses I receive have the property \"_links\" instea

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 03:11

    I have notice the problem appears at least when you trying to return an object extends ResourseSupport vs object containing object extends ResourseSupport. You may return even List or Array of object(s) extends ResourseSupport and have the same effect. See the example :

    @RequestMapping(method = GET, value = "/read")
    public NfcCommand statusPayOrder() {
        return generateNfcCommand();
    }
    

    have response:

    {
        "field": "123",
        "_links": {
            "self": {
                "href": "http://bla_bla_bla_url"
            }
        }
    }
    

    When try to wrap as List:

    @RequestMapping(method = GET, value = "/read")
    public List statusPayOrder() {
        return Arrays.asList(generateNfcCommand());
    }
    

    getting:

    [
        {
            "field": 123
            "links": [
                {
                    "rel": "self",
                    "href": "http://bla_bla_bla_url"
                }
            ]
        }
    ]
    

    Changing structure of an answer is not the right decision, but we can try to think further this way.

提交回复
热议问题