Jack
Jack

Reputation: 1

Passsing List to Controller in Java or Spring?

I have the following method and I need to pass List<UUID> to this Controller method.

@GetMapping("/product/{productUuidList}")
public ResponseEntity<ApiResponse<List<ProductDTO>>> getProductList(
        @RequestParam List<UUID> productUuidList) {

        // code omitted
}

If I pass the list parameters by separating comma, it is ok. But, I want to pass these list parameters as Json array. When I try to pass uuid parameters in Postman as shown below, I get "productUuidList parameter is missing" error.

{ 
    "productUuidList": ["c849bcbb-c26c-4299-9ca4-dcde56830f5f", "398ec0f8-86c8-400a-93cb-caf47c1ac92d"]
}

So, how should I properly pass uuid array to this Controller method without changing @GetMapping to @PostMapping ?

Upvotes: 0

Views: 1133

Answers (1)

GiZeus
GiZeus

Reputation: 86

It won't work like this. Here you are sending a JSON object that looks like this java POJO :

public class MyUUIDWrapper{

    private List<UUID> productUuidList;
    // GETTERS/SETTERS  

}

So Spring is expecting to retrieve a MyUUIDWrapper object in the RequestBody

If you change your code like this it should work :

public ResponseEntity<ApiResponse<List<ProductDTO>>> getProductList(
    @RequestParam MyUUIDWrapper uuids) {

NB : If you have troubles deserializing UUIDs (I've never done it before), change List to List. Not the smartest or most beautiful solution, but you can still convert them later in your controller ;)

Upvotes: 1

Related Questions