[英]How to configure Spring to deserialize JSON into a BigDecimal using a @RequestBody annotation
My BigDecimals are null when I post to the endpoint "/test". 当我发布到端点“ / test”时,我的BigDecimals为空。
Payload that I am posting: 我发布的有效载荷:
{
"decimalOne": "230.0",
"decimalTwo": "215.0"
}
MyObject class: MyObject类:
public class MyObject {
private BigDecimal decimalOne;
private BigDecimal decimalTwo;
public MyObject() {
}
public MyObject(BigDecimal decimalOne, BigDecimal decimalTwo) {
this.decimalOne = decimalOne;
this.decimalTwo = decimalTwo;
}
BigDecimal getDecimalOne() {
return decimalOne;
}
BigDecimal getDecimalTwo() {
return decimalTwo;
}
}
Controller: 控制器:
@RestController
@RequestMapping("/test")
public class MyObjectController {
private DecimalService decimalService;
@Inject
MyObjectController(DecimalService decimalService){
this.decimalService = decimalService;
}
@PostMapping
public Integer getNumberBack(@RequestBody MyObject myObjectPayload){
return decimalService.getNumber(myObjectPayload);
}
}
How do I get Spring to deserialize the JSON into BigDecimal. 如何获取Spring将JSON反序列化为BigDecimal。 Also please let me know if I left out any information.
另外,如果我遗漏了任何信息,请告诉我。 Thanks!
谢谢!
您需要将设置器添加到MyObject,因为使用MyObject()反序列化器创建对象后,没有合法的方式设置字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.