当我发布到端点“/test”时,我的 BigDecimals 为空。
我发布的有效负载:
{
"decimalOne": "230.0",
"decimalTwo": "215.0"
}
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);
}
}
如何让 Spring 将 JSON 反序列化为 BigDecimal。如果我遗漏了任何信息,也请告诉我。谢谢!
最佳答案
您需要向 MyObject 添加 setter ,因为使用 MyObject() 反序列化器创建对象后没有合法的方法来设置字段。
关于java - 如何配置 Spring 使用 @RequestBody 注释将 JSON 反序列化为 BigDecimal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56429426/