2

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.

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thats a wonderful suggestion . Can you please tell me what will you suggest when my controller looks something like this:
Can u please suggest what if my controller looks like : @RequestMapping(method=RequestMethod.POST) public <custom Response Bean POJO> getResults(@Valid User user, BindingResult bindingResult) {
With public ResponseEntity , you can return your POJO normally. return new ResponseEntity<>(yourPOJO, HttpStatus.OK); Take a look here: stackoverflow.com/questions/26134331/…
0

@Albert-Pinto first of all the way you are trying to do this is completely wrong. If you want to consume a object it should come as @RequestBody and not as just a simple object. What you have done in your example is a MVC way which we do with the web applications, not the way we do for web service. so the above code will become as

@Controller @RequestMapping("/user") public class UserController { @RequestMapping(method=RequestMethod.POST) public ResponseEntity create(@RequestBody User user) { try { throw new CustomException(); } catch(CustomException e) { e.printStackTrace(); return new ResponseEntity<String>(e.getErrorDescription(),e.getStatusCode()); } } 

Comments

0

Its is as simple as that 1. Create a class which extends Exception.

 class MyCustomException extends Exception{ MyCustomException(){ } MyCustomException(Object e){ super(e) } } 

2. Make your code throw same type of exception

@Controller @RequestMapping("/user") public class UserController { @RequestMapping(method=RequestMethod.POST) public ResponseEntity create(@Valid User user, BindingResult bindingResult) { try{ if (bindingResult.hasErrors()) { throw new MyCustomException(); } } catch(MyCustomException e){ //do what ever you want to do with it } ... } 

3. Man you already done... :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.