Spring MVC映射请求主体到基本类型

Emd*_*won 1 spring spring-mvc spring-boot

我想通过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);
}
Run Code Online (Sandbox Code Playgroud)

但是这段代码抛出异常:

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
Run Code Online (Sandbox Code Playgroud)

我的请求正文:

{
    "level" : 100
}
Run Code Online (Sandbox Code Playgroud)

Ame*_*bsa 6

您在有效负载中发送的对象不是整数,只需发送数字

100
Run Code Online (Sandbox Code Playgroud)