6
@RequestMapping(..)
@ResponseBody
public SearchResponse search(SearchRequest request) throws SearchException { .. }

I want to return to the requester:

  • JSON-serialized search response in case everything is fine
  • JSON-serialized exception details if SearchException is thrown.

The first part works fine (having Jackson on the classpath and <mvc:annotation-driven />) but I don't see an easy way to serialize the exception as json.

I can register a custom handler, and write a JSON string from there, but I may also need XML serialization, which will mean I'll need conditionals in the handler. Isn't there something ready for that?

1

1 Answer 1

4

Here's what worked: I took axtavt's suggestion (annotating it with @Component), and added this method to the controller:

@ExceptionHandler(CustomException.class)
public @ResponseBody CustomException handleException(CustomException ex) {
    return ex;
}
Sign up to request clarification or add additional context in comments.

1 Comment

It works since Spring 3.1, it doesn't work under Spring 3.0 : jira.spring.io/browse/SPR-6902

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.