#SpringBoot List Validation

3 messages · Page 1 of 1 (latest)

rustic tendon
#

Howdy everybody!

How are you supposed to validate objects in a list being passed to a controller in SpringBoot? I am trying to use the @Valid @NotBlank etc validation annotations but they seem to only work on individual beans. I have seen people suggesting to wrap a list in a custom class but then wouldn't that just be validating the wrapper class and not the internal bean?

Here is an example:

@PostMapping("subtotalBeforeDiscount")
public ResponseEntity<BigDecimal> getSubtotalBeforeDiscount(@RequestBody List<Item> items) {
    log.info("/subtotalBeforeDiscount, executing SubtotalBeforeDiscountService");
    return new ResponseEntity<>(subtotalBeforeDiscountService.calculateSubtotalBeforeDiscountService(items), HttpStatus.OK);
}

I want to validate Item in the params, and I have all of the data validation annotations in the Item class.

Am I approaching validation wrong?

soft pathBOT
#

This post has been reserved for your question.

Hey @rustic tendon! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.