How do I handle or generate an exception if path variable is missing in rest API URL?
I am using spring 4.3.2. I was under the impression that MissingPathVariableException will be thrown. But it's not throwing that exception.
Controller method:
@RequestMapping(method = RequestMethod.GET, value="favorite/all/{userCd}")
public List<UserApplicationDTO> getAllUserFavorites(@PathVariable("userCd") String userCd) throws MyException{
Validation.checkNotNull(userCd,message.getNotNullMessageUser());
return userFavoriteService.getAllUserFavorites(userCd);
}
Handler method:
@Override
protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers,
HttpStatus status, WebRequest request) {
String error = ex.getParameter() + " parameter is missing";
ErrorMessageHandler apiError =message(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), error) ;
return new ResponseEntity<Object>(apiError, new HttpHeaders(), HttpStatus.BAD_REQUEST);
}