I have already looked at a bunch of existing pages on stackoverflow for this but none of these helped:
How to customize @RequestParam error 400 response in Spring MVC
How to inform callers when required rquest parameter is missing?
My problem is extremely similar:
- My application is really a REST api, returning json/xml data rather that html (ie I don't use jsps, but marshalling to transform java beans to json/xml)
- When I query the URL with a required parameter, I get a 400 status with nothing in the payload, no text, nada.
- I would like to return a json/xml payload using my ErrorResponse object that I use in other places.
- I am using Spring 3.2.5
I have a controller which maps a URL to a method with a required parameter(candidateBillDay)
@Controller public class AccountController extends WsController { private static final String JSP_CANDIDATE_PDD = "candidatepdd"; @RequestMapping( value="/account/{externalId}/{externalIdType}/candidatepdd" , method = RequestMethod.GET) public String getCandidatePaymentDueDateInfo( ModelMap model , @PathVariable String externalId , @PathVariable Integer externalIdType , @RequestParam Integer candidateBillDay , @RequestParam(required=false) Boolean includeCurrent ){ ... model.addAttribute( CandidatePaymentDueDateResponse.ROOT_ELEMENT, ...)); return JSP_CANDIDATE_PDD; } }
I have an exception handler that catches all types of exceptions, has some logic to do some specific bits for some types (instanceof):
@ControllerAdvice public class BWSExceptionHandler extends ResponseEntityExceptionHandler { private static final Logger LOG = LoggerFactory.getLogger(BWSExceptionHandler.class); @ExceptionHandler(value = { Exception.class } ) public ResponseEntity<Object> handleOtherExceptions(final Exception ex, final WebRequest req) { LOG.error("Uncaught Exception: ", ex); ErrorResponse resp = null; ... if( ex instanceof MissingServletRequestParameterException ){ MissingServletRequestParameterException e = (MissingServletRequestParameterException)ex; resp = new ErrorResponse( Validatable.ERR_CODE_FIELD_NOT_POPULATED , String.format( Validatable.MSG_FIELD_IS_REQUIRED , e.getParameterName() ) ); httpStatusCode = HttpStatus.BAD_REQUEST; } if(resp==null){ resp = new ErrorResponse(new ErrorElement("unknown_error", ex.getMessage())); } return handleExceptionInternal(ex, resp, new HttpHeaders(), httpStatusCode, req); } }
So this doesn't do anything when a parameter is missing. When I get an actual exception (ie account doesn't exist) then it does catch the exception and works as excepted. This leads me to think that no MissingServletRequestParameterException
exception is thrown, which according to the doc, blogs and stackoverflow pages I've read should be thrown...
I have also tried implementing a class that extends DefaultHandlerExceptionResolver
and override the handleMissingServletRequestParameter
method with not much success ( following this blog: http://alexcuesta.wordpress.com/2011/05/11/error-handling-and-http-status-codes-with-spring-mvc/ )
Any idea of what I am doing wrong or what other option should I explore?