Spring MVC 将请求正文映射到基元类型



我想通过 spring mvc 处理简单的 POST 请求。请求正文仅包含一个 int 值。谁能帮我写动作方法。我的代码如下:

@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

最新更新