Throwing exceptions from Spring REST webservice as JSON/XML

Albert Pinto

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.

Raphael Amoedo

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Throwing exceptions from JBPM WorkItemHandlers?

From Dev

Throwing exceptions from JBPM WorkItemHandlers?

From Dev

Throwing Exceptions method from method

From Dev

spring : get response as Multipart File from REST WebService

From Dev

Throwing exceptions in spring batch and failing gracefully

From Dev

Spring Boot HTTP status without throwing exceptions

From Dev

Subclassing from classses with Java Constructors throwing Exceptions

From Dev

c# - Throwing exceptions from attribute constructor

From Dev

ArangoDB Transactions - How prevent from throwing exceptions

From Dev

Handle MediaPlayer exceptions from throwing infinite errors

From Dev

Handle MediaPlayer exceptions from throwing infinite errors

From Dev

Stacktrace of exceptions in Spring Rest responses

From Dev

Spring Boot REST WebService + JPA : pageable and filter

From Dev

Spring REST webservice serializing to multiple JSON formats

From Dev

Spring REST webservice serializing to multiple JSON formats

From Dev

How to create and destroy session in Spring REST Webservice called from Mobile client

From Dev

Using @Valid is throwing exceptions & not working in basic Spring 3.0 MVC program

From Dev

Using @Valid is throwing exceptions & not working in basic Spring 3.0 MVC program

From Dev

How to get the cookie value from a Rest Webservice

From Dev

Can I prevent java from throwing certain exceptions?

From Dev

Angularjs - Spring MVC Rest : how to handle exceptions

From Dev

Handling exceptions in Spring MVC along with Rest API

From Dev

EJBCA Webservice Exceptions

From Dev

The point of throwing exceptions

From Dev

Join attempt throwing exceptions

From Dev

Duplicating code for throwing exceptions

From Dev

Throwing own exceptions

From Dev

LayoutInflater throwing exceptions

From Dev

Throwing exceptions multiple times

Related Related

  1. 1

    Throwing exceptions from JBPM WorkItemHandlers?

  2. 2

    Throwing exceptions from JBPM WorkItemHandlers?

  3. 3

    Throwing Exceptions method from method

  4. 4

    spring : get response as Multipart File from REST WebService

  5. 5

    Throwing exceptions in spring batch and failing gracefully

  6. 6

    Spring Boot HTTP status without throwing exceptions

  7. 7

    Subclassing from classses with Java Constructors throwing Exceptions

  8. 8

    c# - Throwing exceptions from attribute constructor

  9. 9

    ArangoDB Transactions - How prevent from throwing exceptions

  10. 10

    Handle MediaPlayer exceptions from throwing infinite errors

  11. 11

    Handle MediaPlayer exceptions from throwing infinite errors

  12. 12

    Stacktrace of exceptions in Spring Rest responses

  13. 13

    Spring Boot REST WebService + JPA : pageable and filter

  14. 14

    Spring REST webservice serializing to multiple JSON formats

  15. 15

    Spring REST webservice serializing to multiple JSON formats

  16. 16

    How to create and destroy session in Spring REST Webservice called from Mobile client

  17. 17

    Using @Valid is throwing exceptions & not working in basic Spring 3.0 MVC program

  18. 18

    Using @Valid is throwing exceptions & not working in basic Spring 3.0 MVC program

  19. 19

    How to get the cookie value from a Rest Webservice

  20. 20

    Can I prevent java from throwing certain exceptions?

  21. 21

    Angularjs - Spring MVC Rest : how to handle exceptions

  22. 22

    Handling exceptions in Spring MVC along with Rest API

  23. 23

    EJBCA Webservice Exceptions

  24. 24

    The point of throwing exceptions

  25. 25

    Join attempt throwing exceptions

  26. 26

    Duplicating code for throwing exceptions

  27. 27

    Throwing own exceptions

  28. 28

    LayoutInflater throwing exceptions

  29. 29

    Throwing exceptions multiple times

HotTag

Archive