BAEL-1217 Guide to Spring Type Conversions
Review comments
This commit is contained in:
@@ -5,6 +5,7 @@ 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 {
|
||||
@@ -30,7 +31,9 @@ public class GenericBigDecimalConverter implements GenericConverter {
|
||||
return new BigDecimal(number);
|
||||
} else {
|
||||
Number number = (Number) source;
|
||||
return new BigDecimal(number.doubleValue());
|
||||
BigDecimal converted = new BigDecimal(number.doubleValue());
|
||||
converted.setScale(2, BigDecimal.ROUND_HALF_EVEN);
|
||||
return converted;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+21
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user