package com.logicbig.example;
import java.math.BigDecimal;
import java.time.LocalDate;
public class Order {
private LocalDate orderDate;
private BigDecimal orderPrice;
public LocalDate getOrderDate() {
return orderDate;
}
public void setOrderDate(LocalDate orderDate) {
this.orderDate = orderDate;
}
public BigDecimal getOrderPrice() {
return orderPrice;
}
public void setOrderPrice(BigDecimal orderPrice) {
this.orderPrice = orderPrice;
}
}
package com.logicbig.example;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import java.math.BigDecimal;
public class OrderValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return Order.class == clazz;
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmpty(errors, "orderDate", "date.empty",
"Date cannot be empty");
ValidationUtils.rejectIfEmpty(errors, "orderPrice", "price.empty",
"Price cannot be empty");
Order order = (Order) target;
if (order.getOrderPrice() != null &&
order.getOrderPrice().compareTo(BigDecimal.ZERO) <= 0) {
errors.rejectValue("orderPrice", "price.invalid", "Price must be greater than 0");
}
}
}
This example prints the error codes registered for each fields.
package com.logicbig.example;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.ValidationUtils;
import java.math.BigDecimal;
import java.util.Arrays;
@Configuration
public class SimpleValidationExample {
@Bean
OrderValidator validator() {
return new OrderValidator();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(SimpleValidationExample.class);
Order order = new Order();
order.setOrderPrice(BigDecimal.ZERO);
BeanPropertyBindingResult bindingResult =
new BeanPropertyBindingResult(order, Order.class.getName());
OrderValidator validator = context.getBean(OrderValidator.class);
ValidationUtils.invokeValidator(validator, order, bindingResult);
bindingResult.getAllErrors().forEach(e -> {
Arrays.stream(e.getCodes()).forEach(System.out::println);
});
}
}
Output
date.empty.com.logicbig.example.Order.orderDate
date.empty.orderDate
date.empty.java.time.LocalDate
date.empty
price.invalid.com.logicbig.example.Order.orderPrice
price.invalid.orderPrice
price.invalid.java.math.BigDecimal
price.invalid
Original Post
DefaultMessageCodesResolver#setMessageCodeFormatter(MessageCodeFormatter) Example
Using POSTFIX_ERROR_CODE. Default is POSTFIX_ERROR_CODE.
package com.logicbig.example;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.DefaultMessageCodesResolver;
import org.springframework.validation.ValidationUtils;
import java.math.BigDecimal;
import java.util.Arrays;
@Configuration
public class CustomizedCodesResolverExample {
@Bean
OrderValidator validator() {
return new OrderValidator();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(CustomizedCodesResolverExample.class);
Order order = new Order();
order.setOrderPrice(BigDecimal.ZERO);
BeanPropertyBindingResult bindingResult =
new BeanPropertyBindingResult(order, Order.class.getName());
DefaultMessageCodesResolver messageCodesResolver =
(DefaultMessageCodesResolver) bindingResult.getMessageCodesResolver();
messageCodesResolver.setMessageCodeFormatter(
DefaultMessageCodesResolver.Format.POSTFIX_ERROR_CODE);
OrderValidator validator = context.getBean(OrderValidator.class);
ValidationUtils.invokeValidator(validator, order, bindingResult);
bindingResult.getAllErrors().forEach(e -> {
Arrays.stream(e.getCodes()).forEach(System.out::println);
});
}
}
Output
com.logicbig.example.Order.orderDate.date.empty
orderDate.date.empty
java.time.LocalDate.date.empty
date.empty
com.logicbig.example.Order.orderPrice.price.invalid
orderPrice.price.invalid
java.math.BigDecimal.price.invalid
price.invalid
Original Post
Showing error messages emitted in the validator implementation:
package com.logicbig.example;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.DefaultMessageCodesResolver;
import org.springframework.validation.ValidationUtils;
import java.math.BigDecimal;
import java.util.Locale;
@Configuration
public class CodeToMessagesExample {
@Bean
OrderValidator validator() {
return new OrderValidator();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(CodeToMessagesExample.class);
Order order = new Order();
order.setOrderPrice(BigDecimal.ZERO);
BeanPropertyBindingResult bindingResult =
new BeanPropertyBindingResult(order, Order.class.getName());
DefaultMessageCodesResolver messageCodesResolver =
(DefaultMessageCodesResolver) bindingResult.getMessageCodesResolver();
messageCodesResolver.setMessageCodeFormatter(
DefaultMessageCodesResolver.Format.POSTFIX_ERROR_CODE);
OrderValidator validator = context.getBean(OrderValidator.class);
ValidationUtils.invokeValidator(validator, order, bindingResult);
bindingResult.getAllErrors()
.forEach(e -> System.out.println(context.getMessage(e, Locale.US)));
}
}
Output
Date cannot be empty
Price must be greater than 0
Original Post
Showing error messages from resource bundle:
package com.logicbig.example;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.DefaultMessageCodesResolver;
import org.springframework.validation.ValidationUtils;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Locale;
@Configuration
public class ResourceBundleMessageExample {
@Bean
OrderValidator validator() {
return new OrderValidator();
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("ValidationMessages");
return messageSource;
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ResourceBundleMessageExample.class);
Order order = new Order();
order.setOrderPrice(BigDecimal.ZERO);
BeanPropertyBindingResult bindingResult =
new BeanPropertyBindingResult(order, Order.class.getName());
DefaultMessageCodesResolver messageCodesResolver =
(DefaultMessageCodesResolver) bindingResult.getMessageCodesResolver();
messageCodesResolver.setMessageCodeFormatter(
DefaultMessageCodesResolver.Format.POSTFIX_ERROR_CODE);
OrderValidator validator = context.getBean(OrderValidator.class);
ValidationUtils.invokeValidator(validator, order, bindingResult);
bindingResult.getAllErrors()
.stream()
.map(e -> context.getMessage(e, Locale.US))
.sorted(Comparator.naturalOrder())
.forEach(System.err::println);
}
}
Output
Order Date must be non-empty.
Order Price must be zero or less.
src/main/resources/ValidationMessages_en.properties:
orderDate.date.empty=Order Date must be non-empty.
orderPrice.price.empty=Order Price must be non-empty.
orderPrice.price.invalid=Order Price must be zero or less.
Original Post