I have a POJO with one of the fields mapped using hibernate:
@Enumerated(EnumType.STRING)
@Column(name = "status")
private UserStatus status;
The enum has only 2 possible values: Activated and Deactivated
In my Controller, I have this simple method for saving the entity in the db:
@PostMapping("/save")
private UserDto saveUser(@RequestBody User user){
return userService.save(user);
}
If I try to save the user with an inexistent UserStatus I get the following error message:
"message": "JSON parse error: Cannot deserialize value of type `com.example.demo.utils.UserStatus` from String \"Deactivated1\": value not one of declared Enum instance names: [Deactivated, Activated]; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `com.example.demo.utils.UserStatus` from String \"Deactivated1\": value not one of declared Enum instance names: [Deactivated, Activated]\n at [Source: (PushbackInputStream); line: 3, column: 12] (through reference chain: com.example.demo.entity.User[\"status\"])",
How can I handle the serialization of inexistent values? I want to throw a custom exception in this case. Thank you!