From 7ad1e5692b284e4602c99e6f5c02c8f1952f8123 Mon Sep 17 00:00:00 2001 From: Yasin Bhojawala Date: Sat, 28 Oct 2017 20:41:20 +0530 Subject: [PATCH] BAEL-1217 Guide to Spring Type Conversions --- spring-boot/.gitignore | 1 - .../java/org/baeldung/config/WebConfig.java | 11 +++++ .../converter/GenericBigDecimalConverter.java | 36 +++++++++++++++ .../converter/StringToEmployeeConverter.java | 14 ++++++ .../converter/CustomConverterTest.java | 45 +++++++++++++++++++ 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 spring-boot/src/main/java/org/baeldung/converter/GenericBigDecimalConverter.java create mode 100644 spring-boot/src/main/java/org/baeldung/converter/StringToEmployeeConverter.java create mode 100644 spring-boot/src/test/java/org/baeldung/converter/CustomConverterTest.java diff --git a/spring-boot/.gitignore b/spring-boot/.gitignore index a64317df5e..e26d6af438 100644 --- a/spring-boot/.gitignore +++ b/spring-boot/.gitignore @@ -2,4 +2,3 @@ .settings/ .classpath .project -/.apt_generated/ diff --git a/spring-boot/src/main/java/org/baeldung/config/WebConfig.java b/spring-boot/src/main/java/org/baeldung/config/WebConfig.java index 4ef407823e..6609791c69 100644 --- a/spring-boot/src/main/java/org/baeldung/config/WebConfig.java +++ b/spring-boot/src/main/java/org/baeldung/config/WebConfig.java @@ -1,7 +1,11 @@ package org.baeldung.config; +import org.baeldung.converter.GenericBigDecimalConverter; +import org.baeldung.converter.StringToEnumConverterFactory; +import org.baeldung.converter.StringToEmployeeConverter; import org.baeldung.web.resolver.HeaderVersionArgumentResolver; import org.springframework.context.annotation.Configuration; +import org.springframework.format.FormatterRegistry; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @@ -14,4 +18,11 @@ public class WebConfig extends WebMvcConfigurerAdapter { public void addArgumentResolvers(final List argumentResolvers) { argumentResolvers.add(new HeaderVersionArgumentResolver()); } + + @Override + public void addFormatters(FormatterRegistry registry) { + registry.addConverter(new StringToEmployeeConverter()); + registry.addConverterFactory(new StringToEnumConverterFactory()); + registry.addConverter(new GenericBigDecimalConverter()); + } } diff --git a/spring-boot/src/main/java/org/baeldung/converter/GenericBigDecimalConverter.java b/spring-boot/src/main/java/org/baeldung/converter/GenericBigDecimalConverter.java new file mode 100644 index 0000000000..da46c43dad --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/converter/GenericBigDecimalConverter.java @@ -0,0 +1,36 @@ +package org.baeldung.converter; + +import com.google.common.collect.ImmutableSet; +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.converter.GenericConverter; + +import java.math.BigDecimal; +import java.util.Set; + +public class GenericBigDecimalConverter implements GenericConverter { + @Override + public Set getConvertibleTypes () { + + ConvertiblePair[] pairs = new ConvertiblePair[] { + new ConvertiblePair(Number.class, BigDecimal.class), + new ConvertiblePair(String.class, BigDecimal.class)}; + + return ImmutableSet.copyOf(pairs); + } + + @Override + public Object convert (Object source, TypeDescriptor sourceType, + TypeDescriptor targetType) { + if (sourceType.getType() == BigDecimal.class) { + return source; + } + + if(sourceType.getType() == String.class) { + String number = (String) source; + return new BigDecimal(number); + } else { + Number number = (Number) source; + return new BigDecimal(number.doubleValue()); + } + } +} diff --git a/spring-boot/src/main/java/org/baeldung/converter/StringToEmployeeConverter.java b/spring-boot/src/main/java/org/baeldung/converter/StringToEmployeeConverter.java new file mode 100644 index 0000000000..d7356323ee --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/converter/StringToEmployeeConverter.java @@ -0,0 +1,14 @@ +package org.baeldung.converter; + + +import com.baeldung.toggle.Employee; +import org.springframework.core.convert.converter.Converter; + +public class StringToEmployeeConverter implements Converter { + + @Override + public Employee convert(String from) { + String[] data = from.split(","); + return new Employee(Long.parseLong(data[0]), Double.parseDouble(data[1])); + } +} diff --git a/spring-boot/src/test/java/org/baeldung/converter/CustomConverterTest.java b/spring-boot/src/test/java/org/baeldung/converter/CustomConverterTest.java new file mode 100644 index 0000000000..eda05a8441 --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/converter/CustomConverterTest.java @@ -0,0 +1,45 @@ +package org.baeldung.converter; + +import com.baeldung.toggle.Employee; +import org.baeldung.Application; +import org.baeldung.domain.Modes; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.convert.ConversionService; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +import java.math.BigDecimal; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = Application.class) +@WebAppConfiguration +public class CustomConverterTest { + + @Autowired + ConversionService conversionService; + + @Test + public void whenConvertStringToIntegerUsingDefaultConverter_thenSuccess() { + System.out.println(conversionService.convert("25", Integer.class)); + } + + @Test + public void whenConvertStringToEmployee_thenSuccess() { + System.out.println(conversionService.convert("1,50000.00", Employee.class)); + } + + @Test + public void whenConvertStringToEnum_thenSuccess() { + System.out.println(conversionService.convert("ALPHA", Modes.class)); + } + + @Test + public void whenConvertingToBigDecimalUsingGenericConverter_thenSuccess() { + System.out.println(conversionService.convert(Integer.valueOf(11), BigDecimal.class)); + System.out.println(conversionService.convert(Double.valueOf(25.23), BigDecimal.class)); + System.out.println(conversionService.convert("2.32", BigDecimal.class)); + } +}