How to distinguish if a Java Enum exists or was incorrectly given in Spring Boot

Viewed 33

There's a PATCH method that updates many fields of an item. One of the (optional) fields is the status (an Enum).

public class ItemUpdate {
    // ...
    public enum Status {
        A("a"),
        B("b");
        // ...
    }
}

However I want to be able to distinguish if the Enum was incorrectly given (e.g. "c") or it wasn't even sent in the request body. In both scenarios the log.info() writes null.

@RequestMapping(
    value = "/item/{id}",
    method = RequestMethod.PATCH
)
public ResponseEntity<Item> patchItem(
    @PathVariable("id") String id, 
    @RequestBody ItemUpdate itemUpdate
) {
    log.info( String.format("status enum = %s", itemUpdate.getStatus()) );
    // ...
}
0 Answers
Related