BAEL-1217 Guide to Spring Type Conversions

This commit is contained in:
Yasin Bhojawala 2017-10-28 20:41:20 +05:30
parent 26f38c1b69
commit 7ad1e5692b
5 changed files with 106 additions and 1 deletions

View File

@ -2,4 +2,3 @@
.settings/
.classpath
.project
/.apt_generated/

View File

@ -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<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new HeaderVersionArgumentResolver());
}
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToEmployeeConverter());
registry.addConverterFactory(new StringToEnumConverterFactory());
registry.addConverter(new GenericBigDecimalConverter());
}
}

View File

@ -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<ConvertiblePair> 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());
}
}
}

View File

@ -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<String, Employee> {
@Override
public Employee convert(String from) {
String[] data = from.split(",");
return new Employee(Long.parseLong(data[0]), Double.parseDouble(data[1]));
}
}

View File

@ -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));
}
}