我有如下所示的有效负载。
{
"name": "XXXX",
"expMonth": 07,
"expYear": 21
}
我的请求正文 POJO 如下所示。
public Class Card {
private String name;
private Integer expMonth;
private Integer expYear;
}
但是,expMonth
未正确绑定(bind)并引发异常。请提出前进的方向。
异常堆栈:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Invalid numeric value: Leading zeroes not allowed; nested exception is com.fasterxml.jackson.core.JsonParseException: Invalid numeric value: Leading zeroes not allowed at [Source: (PushbackInputStream); line: 4, column: 16]
最佳答案
However, expMonth is not binding properly and throws an exception.
根据异常堆栈,您有前导零,因此 Jackson 抛出了异常。您可以尝试类似的方法,
{
"name": "XXXX",
"expMonth": 7, <--- Removing the leading zero(s).
"expYear": 21
}
另一种方法是将其更改为 String,
{
"name": "XXXX",
"expMonth": "07",
"expYear": 21
}
代码:
public Class Card {
private String name;
private String expMonth;
private Integer expYear;
}
然后你可以做类似的事情,
Integer.parseInt(expMonth);
关于java - 如何在 Spring boot 中将带有前导零的整数绑定(bind)到请求负载中的整数字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56759680/