I have a REST web service controller that looks like this:
@RequestMapping(value = URIConstants.URL_DOCUMENT_SEARCH, method = RequestMethod.POST, produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE })
protected DocumentSearchResponse getDocuments(@Valid @ModelAttribute DocumentSearchRequest objDMSRequest,BindingResult bindingResult, HttpServletRequest objServletRequest) throws AppException
{
if (bindingResult.hasErrors())
{
//I want to throw my custom exception here
///Or can anyone suggest a more clean and efficient way
}
-----More code and logic
}
I have a custom exception and handlers that will throw invalid HTTP invalid request exception. The custom exception has errorcode and error description fields. My requirement is is there a way to parse the error from the bindingresults to a custome exception and trow that in the controler.
What you can do:
return new ResponseEntity<String>(errorDescription,HttpStatus.BAD_REQUEST);
Or, you can do it hardcore if you really want to use Exception(not recommended):
try {
throw new CustomException();
} catch(CustomException e) {
e.printStackTrace();
return new ResponseEntity<String>(e.getErrorDescription(),e.getStatusCode());
}
By the way: returning a Exception it's not good, that's why I don't show it.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments