Spring MVC Map Request Body to Primitive Type

Viewed 3434

I want to handle simple POST request by spring mvc. The request body contain only a single int value. Can anyone help me to write the action method. My code is below:

@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);
}

But this code throw exception:

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

My request body:

{
    "level" : 100
}
1 Answers
Related