How to post a list to Spring Data Rest?

后端 未结 4 1391
情书的邮戳
情书的邮戳 2021-01-04 03:17

I followed this example, which allows to post a unique Person object. I want a REST service where I can post a collection of Person at once, e.g. a

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 04:03

    I tried to use @RequestBody List>. When the request body does not contain any links, it works well, but if the element carries a link, the server could not deserialize the request body.

    Then I tried to use @RequestBody Resources, but I could not figure out the default name of a list.

    Finally, I tried a wrapper which contained List>, and it works.

    Here is my solution:

    First create a wrapper class for List>:

    public class Bulk {
        private List> bulk;
        // getter and setter
    }
    

    Then use @RequestBody Resource> for parameters.

    Finally, example json with links for create bulk data in one request:

    {
        "bulk": [
            {
                "title": "Spring in Action",
                "author": "http://localhost:8080/authors/1"
            },
            {
                "title": "Spring Quick Start",
                "author": "http://localhost:8080/authors/2"
            }
        ]
    }
    

提交回复
热议问题