Merge pull request #2893 from yasin3061/master

BAEL-1217 Guide to Spring Type Conversions
This commit is contained in:
lor6 2017-10-29 16:32:09 +02:00 committed by GitHub
commit ff0a23b2f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 117 additions and 1 deletions

View File

@ -2,4 +2,4 @@
.settings/
.classpath
.project
/.apt_generated/
/.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,38 @@
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.math.MathContext;
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;
BigDecimal converted = new BigDecimal(number.doubleValue());
return converted.setScale(2, BigDecimal.ROUND_HALF_EVEN);
}
}
}

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,53 @@
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;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class CustomConverterTest {
@Autowired
ConversionService conversionService;
@Test
public void whenConvertStringToIntegerUsingDefaultConverter_thenSuccess() {
assertThat(conversionService.convert("25", Integer.class)).isEqualTo(25);
}
@Test
public void whenConvertStringToEmployee_thenSuccess() {
Employee employee = conversionService.convert("1,50000.00", Employee.class);
Employee actualEmployee = new Employee(1, 50000.00);
assertThat(conversionService.convert("1,50000.00", Employee.class))
.isEqualToComparingFieldByField(actualEmployee);
}
@Test
public void whenConvertStringToEnum_thenSuccess() {
assertThat(conversionService.convert("ALPHA", Modes.class)).isEqualTo(Modes.ALPHA);
}
@Test
public void whenConvertingToBigDecimalUsingGenericConverter_thenSuccess() {
assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class))
.isEqualTo(BigDecimal.valueOf(11.00).setScale(2, BigDecimal.ROUND_HALF_EVEN));
assertThat(conversionService.convert(Double.valueOf(25.23), BigDecimal.class))
.isEqualByComparingTo(BigDecimal.valueOf(Double.valueOf(25.23)));
assertThat(conversionService.convert("2.32", BigDecimal.class))
.isEqualTo(BigDecimal.valueOf(2.32));
}
}