I know there is some similar questions here about how to parse ENUM, how to parse customize JSON structure. But here my question is how to just give a better message when the user submit some JSON with is not as expected.
This is the code:
@PutMapping
public ResponseEntity updateLimitations(@PathVariable("userId") String userId,
@RequestBody LimitationParams params) {
Limitations limitations = user.getLimitations();
params.getDatasets().forEach(limitations::updateDatasetLimitation);
params.getResources().forEach(limitations::updateResourceLimitation);
userRepository.save(user);
return ResponseEntity.noContent().build();
}
And the request body I expected is like this:
{
"datasets": {"public": 10},
"resources": {"cpu": 2}
}
But when they submit something like this:
{
"datasets": {"public": "str"}, // <--- a string is given
"resources": {"cpu": 2}
}
The response will show something like this in logs:
400 JSON parse error: Cannot deserialize value of type `java.lang.Integer` from String "invalid": not a valid Integer value; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.lang.Integer` from String "invalid": not a valid Integer value
at [Source: (PushbackInputStream); line: 1, column: 23] (through reference chain: com.openbayes.api.users.LimitationParams["datasets"]->java.util.LinkedHashMap["public"])
But what I want is a more human readable message.
I tried to use ExceptionHandler for com.fasterxml.jackson.databind.exc.InvalidFormatException but it doesn't work.