当我向POST
发出@RestController
这样的请求时:
{ "name": undefined }
我收到以下回复:
{
...
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Could not read document: Unrecognized token 'undefined': was expecting ('true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@5f9559a4; line: 3, column: 27]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'undefined': was expecting ('true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@5f9559a4; line: 3, column: 27]",
...
}
我正在寻找一个通用解决方案,告诉杰克逊始终将undefined
映射/反序列化为null
。我还没有找到一个涉及处理未定义值的示例或文档,因此任何指针都会被贬值。
答案 0 :(得分:1)
不要发送undefined。
在目标对象中将值定义为String,然后将其包装在方法中。也许,这样的事情:
private String blammy;
public boolean isBlammyTrue()
{
return "true".equalsIgnoreCase(blammy);
}
public boolean isBlammyFalse()
{
return !isBlammyTrue();
}