BAEL-1217 Guide to Spring Type Conversions
Review comments
This commit is contained in:
parent
89f9e2fed6
commit
e56da99216
@ -5,6 +5,7 @@ import org.springframework.core.convert.TypeDescriptor;
|
|||||||
import org.springframework.core.convert.converter.GenericConverter;
|
import org.springframework.core.convert.converter.GenericConverter;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.math.MathContext;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class GenericBigDecimalConverter implements GenericConverter {
|
public class GenericBigDecimalConverter implements GenericConverter {
|
||||||
@ -30,7 +31,9 @@ public class GenericBigDecimalConverter implements GenericConverter {
|
|||||||
return new BigDecimal(number);
|
return new BigDecimal(number);
|
||||||
} else {
|
} else {
|
||||||
Number number = (Number) source;
|
Number number = (Number) source;
|
||||||
return new BigDecimal(number.doubleValue());
|
BigDecimal converted = new BigDecimal(number.doubleValue());
|
||||||
|
converted.setScale(2, BigDecimal.ROUND_HALF_EVEN);
|
||||||
|
return converted;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
package org.baeldung.converter.controller;
|
||||||
|
|
||||||
|
import com.baeldung.toggle.Employee;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/string-to-employee")
|
||||||
|
public class StringToEmployeeConverterController {
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<Object> getStringToEmployee(@RequestParam("employee") Employee employee) {
|
||||||
|
return ResponseEntity.ok(employee);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(consumes = {MediaType.TEXT_PLAIN_VALUE})
|
||||||
|
public ResponseEntity<Object> postStringToEmployee(@RequestBody Employee employee) {
|
||||||
|
return ResponseEntity.ok(employee);
|
||||||
|
}
|
||||||
|
}
|
@ -13,6 +13,8 @@ import org.springframework.test.context.web.WebAppConfiguration;
|
|||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringBootTest(classes = Application.class)
|
@SpringBootTest(classes = Application.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@ -23,23 +25,29 @@ public class CustomConverterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenConvertStringToIntegerUsingDefaultConverter_thenSuccess() {
|
public void whenConvertStringToIntegerUsingDefaultConverter_thenSuccess() {
|
||||||
System.out.println(conversionService.convert("25", Integer.class));
|
assertThat(conversionService.convert("25", Integer.class)).isEqualTo(25);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenConvertStringToEmployee_thenSuccess() {
|
public void whenConvertStringToEmployee_thenSuccess() {
|
||||||
System.out.println(conversionService.convert("1,50000.00", Employee.class));
|
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
|
@Test
|
||||||
public void whenConvertStringToEnum_thenSuccess() {
|
public void whenConvertStringToEnum_thenSuccess() {
|
||||||
System.out.println(conversionService.convert("ALPHA", Modes.class));
|
assertThat(conversionService.convert("ALPHA", Modes.class)).isEqualTo(Modes.ALPHA);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenConvertingToBigDecimalUsingGenericConverter_thenSuccess() {
|
public void whenConvertingToBigDecimalUsingGenericConverter_thenSuccess() {
|
||||||
System.out.println(conversionService.convert(Integer.valueOf(11), BigDecimal.class));
|
assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class)).
|
||||||
System.out.println(conversionService.convert(Double.valueOf(25.23), BigDecimal.class));
|
isEqualTo(BigDecimal.valueOf(11));
|
||||||
System.out.println(conversionService.convert("2.32", BigDecimal.class));
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
package org.baeldung.converter.controller;
|
||||||
|
|
||||||
|
import org.baeldung.Application;
|
||||||
|
import org.baeldung.boot.DemoApplication;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
//@AutoConfigureTestDatabase
|
||||||
|
public class StringToEmployeeConverterControllerTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getStringToEmployeeTest() throws Exception {
|
||||||
|
mockMvc.perform(get("/string-to-employee?employee=1,2000"))
|
||||||
|
.andDo(print())
|
||||||
|
.andExpect(jsonPath("$.id", is(1)))
|
||||||
|
.andExpect(jsonPath("$.salary", is(2000.0)))
|
||||||
|
.andExpect(status().isOk());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user