我想通过 spring mvc 处理简单的 POST 请求。请求正文仅包含一个 int 值。谁能帮我写一下 Action 方法。我的代码如下:
@PostMapping(value = "/update")
@ResponseBody
public ResponseEntity updateLogLevel(@RequestBody int level) {
try {
//process request
} catch (Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
return ResponseEntity.ok(Constant.STATUS_SUCCESS);
}
但是这段代码抛出异常:
org.springframework.http.converter.HttpMessageNotReadableException",
"message":"JSON parse error: Can not deserialize instance of int out of START_OBJECT token;
nested exception is
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int out of START_OBJECT token
我的请求正文:
{
"level" : 100
}
最佳答案
你在你的有效载荷中发送一个不是整数的对象,只需发送数字
100
关于Spring MVC 将请求主体映射到原始类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45530748/