YoungMoneyMitch
JCHJava Community | Help. Code. Learn.
•Created by YoungMoneyMitch on 4/18/2023 in #java-help
SpringBoot List Validation
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?
4 replies
JCHJava Community | Help. Code. Learn.
•Created by YoungMoneyMitch on 4/17/2023 in #java-help
BigDecimal rounding standards on currency
Howdy everybody! I have been using BigDecimal for a few functions run on currency. I have used it for every intermediate operation and now need to display the final product. What is the standard for rounding? I get that it should be an easy round to 2 decimals for USD, but is the rounding strategy generally to truncate, always round up, or round even?
I have tried finding an answer online and there doesn't seem to be a legal standard or anything like that, but I would assume there is with taxes right? All input is appreciated!
18 replies
JCHJava Community | Help. Code. Learn.
•Created by YoungMoneyMitch on 4/16/2023 in #java-help
Spring Boot Request Body
Howdy Everybody!
I am creating a simple API that takes in two lists that hold different objects. Right now I am using the @RequestBody annotation in the controller to map the data to an object but now I don't know how to do it with different lists. Do I need a wrapper class? If so is it better to make a custom wrapper or use a Map? All help is appreciated!
7 replies