Spring provides a mechanism to use annotations to declaratively define formatting rules.
Spring provides predefined annotations in package: org.springframework.format.annotation.
To bind an annotation to a Formatter, AnnotationFormatterFactory is implemented. For example the predefined annotation @NumberFormat , there is NumberFormatAnnotationFormatterFactory . In the next tutorial we will see how to create our own annotation along with an implementation of AnnotationFormatterFactory
The AnnotationFormatterFactory implementation is then registered with ConversionService
Here's an example of using DateTimeFormat and NumberFormat:
Example
Using predefined format annotation
package com.logicbig.example;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import java.util.Date;
public class Order {
@NumberFormat(style = NumberFormat.Style.PERCENT)
private Double price;
@DateTimeFormat(pattern = "yyyy")
private Date date;
.............
}
Using DataBinder
Here we are going to create ConversionService, configuring it with the AnnotationFormatterFactory instances which binds the DateTimeFormat and NumberFormat annotations with their corresponding formatters, and then Using DataBinder to populate the fields of the target bean.
package com.logicbig.example;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory;
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import java.util.Date;
import java.util.Map;
public class SpringFormatAnnotationExample {
public static void main (String[] args) {
DefaultFormattingConversionService conversionService =
new DefaultFormattingConversionService(false);
conversionService.addFormatterForFieldAnnotation(
new NumberFormatAnnotationFormatterFactory());
conversionService.addFormatterForFieldAnnotation(
new DateTimeFormatAnnotationFormatterFactory());
Order order = new Order();
DataBinder dataBinder = new DataBinder(order);
dataBinder.setConversionService(conversionService);
MutablePropertyValues mpv = new MutablePropertyValues();
mpv.add("price", "2.7%");
mpv.add("date", "2016");
dataBinder.bind(mpv);
Object target = dataBinder.getTarget();
System.out.println(target);
BindingResult bindingResult = dataBinder.getBindingResult();
System.out.println(bindingResult);
}
}
OutputOrder{price=0.027, date=Fri Jan 01 00:00:00 CST 2016} org.springframework.validation.BeanPropertyBindingResult: 0 errors
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 17
- Maven 3.8.1
|