Close

Spring Framework - DataBinder Examples

Spring Framework 

This example shows, how to use DataBinder to do string input to bean property settings. This is considered a higher level alternative to BeanWrapperImpl.

package com.logicbig.example;

public class TestBean {
private int anInt;

public int getAnInt () {
return anInt;
}

public void setAnInt (int anInt) {
this.anInt = anInt;
}

@Override
public String toString () {
return "TestBean{anInt=" + anInt + '}';
}
}

DataBinder setting bean properties:

package com.logicbig.example;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.validation.DataBinder;

public class DataBinderExample {
public static void main (String[] args) {

MutablePropertyValues mpv = new MutablePropertyValues();
mpv.add("anInt", "10");

TestBean testBean = new TestBean();
DataBinder db = new DataBinder(testBean);

db.bind(mpv);
System.out.println(testBean);

}
}

Output

TestBean{anInt=10}
Original Post




Capturing binding results.

package com.logicbig.example;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.validation.DataBinder;

public class DataBinderUsingBindingResultExample {
public static void main (String[] args) {

MutablePropertyValues mpv = new MutablePropertyValues();
mpv.add("anInt", "10x"); // 10x cannot be converted to int type

TestBean testBean = new TestBean();
DataBinder db = new DataBinder(testBean);

db.bind(mpv);
db.getBindingResult().getAllErrors().stream().forEach(System.out::println);
System.out.println(testBean);

}
}

Output

TestBean{anInt=0}
Original Post




This example shows how to use ConversionService with DataBinder and a custom converter

package com.logicbig.example;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.validation.DataBinder;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class ConversionServiceWithDataBinderExample {
public static void main (String[] args) {

MutablePropertyValues mpv = new MutablePropertyValues();
mpv.add("date", new Date());

DataBinder dataBinder = new DataBinder(new MyObject());
DefaultConversionService service = new DefaultConversionService();
service.addConverter(new DateToLocalDateTimeConverter());
//commenting the following line will not populate date field
dataBinder.setConversionService(service);

dataBinder.bind(mpv);
dataBinder.getBindingResult()
.getModel()
.entrySet()
.forEach(System.out::println);
}

private static class MyObject {
private LocalDateTime date;

public LocalDateTime getDate () {
return date;
}

public void setDate (LocalDateTime date) {
this.date = date;
}

@Override
public String toString () {
return "MyObject{" +
"date=" + date +
'}';
}
}

private static class DateToLocalDateTimeConverter
implements Converter<Date, LocalDateTime> {

@Override
public LocalDateTime convert (Date source) {
return LocalDateTime.ofInstant(source.toInstant(),
ZoneId.systemDefault());
}
}
}

Output

target=MyObject{date=2017-05-01T16:07:51.548}
Original Post




See Also