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 PostCapturing 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
Field error in object 'target' on field 'anInt': rejected value [10x]; codes [typeMismatch.target.anInt,typeMismatch.anInt,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.anInt,anInt]; arguments []; default message [anInt]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'anInt'; nested exception is java.lang.NumberFormatException: For input string: "10x"]
TestBean{anInt=0}
Original PostThis 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}