Jim C
Jim C

Reputation: 4385

How to post a list to Spring Data Rest?

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 list/any collection named Team with numerous Person objects in just one call.

I mean, my question is not exactly about the OneToMany relationship, where you send each person in a REST request. This topic is well answered.

I want to send a collection of Person objects taking advantage of @RepositoryRestResource or another feature from Spring Data Rest. Is this possible with Spring Data Rest or should I workaround by creating a controller, receive the list and parse the Team list to insert each Person?

I found this feature request, which seems to answer that nowadays Spring Rest Data is missing what I am looking for, but I am not sure.

In my business requirement, application A will post a list of orders to application B and I have to save it in database for future processing, so, after reading about Spring Data Rest and making some samples, I found its clean architecture amazing and very suitable for my requirement except for the fact that I didn't figure out how to post a list.

Upvotes: 21

Views: 16211

Answers (5)

Jess
Jess

Reputation: 3715

Base the answer of totran, this is my code:

There are dependencies:

springBootVersion = '2.4.2'
springDependencyManagement = '1.0.10.RELEASE'

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

The codes:

import icu.kyakya.rest.jpa.model.Address;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@RepositoryRestResource(collectionResourceRel = "address", path = "address")
public interface AddressRepository extends PagingAndSortingRepository<Address, Long> {
//...
}
import lombok.Data;
import java.util.List;

@Data
public class Bulk<T> {
    private List<T> bulk;
}
import lombok.RequiredArgsConstructor;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.ExposesResourceFor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@BasePathAwareController  // if base url exists, it needs to be added
@RepositoryRestController
@RequiredArgsConstructor
@ExposesResourceFor(Address.class)
public class AddressController {

    private final AddressRepository repo;

    @PostMapping("/address/saveAll")
    public ResponseEntity<Iterable<Address>> saveAll(@RequestBody EntityModel<Bulk<Address>> bulk) {
        List<Address> addresses = Objects.requireNonNull(bulk.getContent()).getBulk();
        Iterable<Address> resp = repo.saveAll(addresses);
        return new ResponseEntity<>(resp,HttpStatus.CREATED);
    }
}

The way more like Spring data rest:

import lombok.RequiredArgsConstructor;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.server.ExposesResourceFor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@BasePathAwareController  // if base url exists, it needs to be added
@RepositoryRestController
@RequiredArgsConstructor
@ExposesResourceFor(Address.class)
public class AddressController {

    private final AddressRepository repo;
    private final RepositoryEntityLinks entityLinks; //get link


    /**
     * curl -i -X POST -H "Content-Type:application/json" -d  '{ "bulk": [ {"country" : "Japan" , "city" : "Tokyo" }, {"country" : "Japan" , "city" : "Osaka" }]} '   http://localhost:8080/api/v1/address/saveAll
     *
     * @return 201 https://docs.spring.io/spring-data/rest/docs/current/reference/html/#repository-resources.default-status-codes
     */
    @PostMapping("/address/saveAll")
    public ResponseEntity<CollectionModel<EntityModel<Address>>>         List<Address> data = Objects.requireNonNull(bulk.getContent()).getBulk();
        Iterable<Address> addresses = repo.saveAll(data);

        ArrayList<EntityModel<Address>> models = new ArrayList<>();
        addresses.forEach(i->{
            Link link = entityLinks.linkToItemResource(Address.class, i.getId()).withRel("self");
            models.add(EntityModel.of(i).add(link));
        });
        return new ResponseEntity<>(CollectionModel.of(models),HttpStatus.CREATED);
    }
}

Upvotes: 0

wotboy
wotboy

Reputation: 19

@RequestMapping(method=RequestMethod.POST, value="/batchInsert", consumes = "application/json", produces = "application/json")
@ResponseBody
public ResponseEntity<?> batchInsert(@RequestBody Resources<Person> people, PersistentEntityResourceAssembler assembler) throws Exception {
    Iterable<Person> s = repo.save( people.getContent() ); // save entities

    List<PersistentEntityResource> list = new ArrayList<PersistentEntityResource>();
    Iterator<Sample> itr = s.iterator();
    while(itr.hasNext()) {
        list.add( assembler.toFullResource( itr.next() ) );
    }

    return ResponseEntity.ok( new Resources<PersistentEntityResource>(list) );
}

Upvotes: 1

Khaled Lela
Khaled Lela

Reputation: 8119

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<Person>> personWrapper,
        PersistentEntityResourceAssembler assembler) {
    Resources<Person> resources = new Resources<Person>(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<Person> content;
   
public List<Person> getContent(){
return content;
}

public void setContent(List<Person> content){
this.content = content;
}
}

public class Person{
private String name;
private String email;
// Other fields

// GETTER & SETTER 
}

Upvotes: 5

user1685095
user1685095

Reputation: 6121

Well, AFAIK you can't do that with spring data rest, just read the docs and you will see, that there is no mention about posting a list to collection resource.

The reason for this is unclear to me, but for one thing - the REST itself doesn't really specify how you should do batch operations. So it's unclear how one should approach that feature, like should you POST a list to collection resource? Or should you export resource like /someentity/batch that would be able to patch, remove and add entities in one batch? If you will add list how should you return ids? For single POST to collection spring-data-rest return id in Location header. For batch add this cannot be done.

That doesn't justify that spring-data-rest is missing batch operations. They should implement this IMHO, but at least it can help to understand why are they missing it maybe.

What I can say though is that you can always add your own Controller to the project that would handle /someentity/batch properly and you can even probably make a library out of that, so that you can use it in another projects. Or even fork spring-data-rest and add this feature. Although I tried to understand how it works and failed so far. But you probably know all that, right?

There is a feature request for this.

Upvotes: 17

totran
totran

Reputation: 89

I tried to use @RequestBody List<Resource<MyPojo>>. 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<MyPojo>, but I could not figure out the default name of a list.

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

Here is my solution:

First create a wrapper class for List<Resource<MyPojo>>:

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

Then use @RequestBody Resource<Bulk<MyPojo>> 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"
        }
    ]
}

Upvotes: 4

Related Questions