Connor Brady
Connor Brady

Reputation: 77

On POST, Pass Object IDs rather than full object

I was wondering if there was a way in Spring Boot to pass the IDs of an object in an Array which Spring Boot will then get the object details for rather than the full object

This is an issue for me as to find the object, it appears Spring Boot wants the full object which will not work in my case

For example

{
    "request": [
                   "e77d8168-4217-43c9-a6dd-fb54957d1302"
               ]
}

Rather than having to pass

{
    "request": [
                   {
                       "uuid": "e77d8168-4217-43c9-a6dd-fb54957d1302",
                       "name": "Object 1"
                   }
               ]
}

Upvotes: 0

Views: 64

Answers (1)

DCTID
DCTID

Reputation: 1337

Yes. The problem is probably with the class you are using to map your List<String>. It must match your json. Your controller should be like:

@RestController
public class IdsController {

    @PostMapping("endpoint")
    public void postIds(@RequestBody IdRequest idRequest){
        idRequest.getRequest().forEach(System.out::println);
    }
}

And the request object:

public class IdRequest {
    private List<String> request;

    public List<String> getRequest() {
        return request;
    }
}

Upvotes: 1

Related Questions