Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring REST - binding GET parameters to list of nested objects

My question is almost 1:1 as this one. The only difference (and struggle) I have is that my "data container" has a collection of objects. It looks like this:

public class A {
    int plainFieldA;
    B fieldB;
    List<B> collectionB = new ArrayList<>();
}

public class B {
    int plainFieldB;
}

@Transactional(readOnly = true)
@GetMapping("")
public Entity getAll(A reqParam) {
    return getAll(reqParam);
}

Is it possible to define collectionB in params of the url http://localhost/api/test?plainFieldA=1 without creating a converter ? @GameSalutes correctly pointed out that since spring 4 we can do fieldB.plainFieldB=2 so the url will be: http://localhost/api/test?plainFieldA=1&fieldB.plainFieldB=2 but the question is can we do soemthing similar with collectionB without creating a converter ?

like image 718
user3529850 Avatar asked Dec 09 '19 20:12

user3529850


1 Answers

Yes, you can make the request like this:

http://localhost/api/test?plainFieldA=1&fieldB.plainFieldB=2&collectionB[0].plainFieldB=9

Or encoding the request, for postman:

http://localhost/api/test?plainFieldA=1&fieldB.plainFieldB=2&collectionB%5B0%5D.plainFieldB=9

With two objects in request:

http://localhost/api/test?plainFieldA=1&fieldB.plainFieldB=2&collectionB%5B0%5D.plainFieldB=9&collectionB%5B1%5D.plainFieldB=11

The result with breakpoint in the IDE:

enter image description here

like image 190
Francesc Recio Avatar answered Sep 30 '22 17:09

Francesc Recio