I have a simple spring rest controller that looks like this.
@RestController
public class MyController {
@RequestMapping(path = "mapping", method = RequestMethod.POST, produces = {"application/json"})
public MyResponse create(@RequestBody MyModel requestParam
) throws InvalidApplicationSentException, AuthenticationFailedException {
// method body
}
Here is the MyModel class that's used as a request parameter.
public class MyModel {
private RequestType requestType;
// a lot of other properties ..
}
Now when i try to call this endpoint passing an invalid value for RequestType I get back an exeption:
org.springframework.http.converter.HttpMessageNotReadableException
Could not read document: Can not construct instance of com.mypackage.RequestType from String value 'UNDEFINED': value not one of declared Enum instance names: [IMPROTANT, NOT_IMPORTANT]
Is there a way that spring would set the enum to null when passed incorrect value and not throw an error?
I'm using spring 4 and I would prefer configuration with annotations and not xml files