Time for action – adding Spring validation
If you have a constraint that doesn't allow anyone to add more than 99 units of any product if the unit price is greater than 1000 USD for that product, add such a validation using Spring validation in the project. Perform the following steps:
Create a class called
UnitsInStockValidator
under thecom.packt.webstore.validator
package in the source foldersrc/main/java
. Add the following code into it:package com.packt.webstore.validator; import java.math.BigDecimal; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.Validator; import com.packt.webstore.domain.Product; @Component public class UnitsInStockValidator implements Validator{ public boolean supports(Class<?> clazz) { return Product.class.isAssignableFrom(clazz); } public void validate(Object target, Errors errors) { Product product = (Product) target; if(product.getUnitPrice()!...