简体   繁体   English

如何配置Spring以使用@RequestBody批注将JSON反序列化为BigDecimal

[英]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.

相关问题 在使用@Requestbody注释在Spring Boot中进行处理之前,如何在数据库或日志文件中记录JSON或XML请求 - How to log JSON or XML request in a database or log file before processing in Spring boot using @Requestbody annotation "Jackson:使用注释将 BigDecimal 反序列化为纯字符串" - Jackson: Deserialize BigDecimal as plain String using annotation 如何在Spring MVC继承中使用@RequestBody批注 - How to use the @RequestBody annotation in Spring MVC inheritance Spring 注解缺少 requestBody - Spring Annotation missing requestBody 弹簧 WebFlux。 如何使用@RequestBody 注释以两种不同格式获取请求正文? - Spring WebFlux. How to get the request body in two different formats using @RequestBody annotation? Spring 安全认证使用 RequestBody 读取 JSON - Spring Security Auth using RequestBody for reading JSON 使用Jackson框架的@RequestBody注释的Spring Boot ObjectMapper - Spring Boot ObjectMapper for @RequestBody annotation using Jackson Framework 在春季使用带有@RequestBody批注的GET请求 - GET request with @RequestBody annotation in Spring 如何在 spring 引导中将 JSON hasmasp 绑定到 @RequestBody - How to bind JSON hasmasp to the @RequestBody in spring boot 在Spring中使用RequestBody解析JSON - Parsing JSON with RequestBody in Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM