How to post a list to Spring Data Rest?

后端 未结 4 1393
情书的邮戳
情书的邮戳 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 03:55

    Based on user1685095 answer, You can make custom Controller PersonRestController and expose post collection of Person as it seem not exposed yet by Spring-date-rest

    @RepositoryRestController
    @RequestMapping(value = "/persons")
    public class PersonRestController {
    private final PersonRepository repo;
    @Autowired
    public AppointmentRestController(PersonRepository repo) {
        this.repo = repo;
    }
    
    @RequestMapping(method = RequestMethod.POST, value = "/batch", consumes = "application/json", produces = "application/json")
    public @ResponseBody ResponseEntity savePersonList(@RequestBody Resource> personWrapper,
            PersistentEntityResourceAssembler assembler) {
        Resources resources = new Resources(repo.save(personWrapper.getContent()));
        //TODO add extra links `assembler`
        return ResponseEntity.ok(resources);
    }
    

    }

    PersonWrapper to fix:

    Can not deserialize instance of org.springframework.hateoas.Resources out of START_ARRAY token\n at [Source: java.io.PushbackInputStream@3298b722; line: 1, column: 1]

    Update

    public class PersonWrapper{
     private List content;
       
    public List getContent(){
    return content;
    }
    
    public void setContent(List content){
    this.content = content;
    }
    }
    
    public class Person{
    private String name;
    private String email;
    // Other fields
    
    // GETTER & SETTER 
    }
    

提交回复
热议问题