3

I was using spring boot with Jersey 1. Used ExceptionMapper to handle the exceptions globally. Now we are planning to migrate to spring MVC for restful purpose. So, changing few of the endpoints to use dispatcher servlet. Is there any way to handle all exceptions using exceptionMapper. There exists ControllerAdvice to handle exceptions globally for spring mvc but is there anyway I can use ExceptionMapper providers for time being?

Thank you

Ajay
  • 4,134
  • 3
  • 20
  • 19
  • Maybe something like this? `@ControllerAdvice public class RestResponseEntityExceptionHandler @Autowired private MyExceptionMapper exceptionMapper; @ExceptionHandler({ Exception.class }) public Response handleException(Exception ex) { return exceptionMapper.toResponse(ex) } }` or do you have multiple `ExceptionMapper`'s for each Exception type? – varren Sep 18 '17 at 00:52
  • 1
    The only way I can think of is to make the ExceptionMapper a Spring bean and inject it into a ControllerAdvice and call the ExceptionMapper from one of the advice's method. You will need to convert the JAX-RS Response to a Spring ResponseEntity. I don't see any other way. This allows for _some_ code reuse, but is is not that great a solution. – Paul Samsotha Sep 18 '17 at 01:07
  • 1
    Or create an abstraction that you can inject into both the advice and the mapper. This will look a lot cleaner. – Paul Samsotha Sep 18 '17 at 06:34
  • @varren Yeah I have quite a few ExceptionMappers for each exceptionType (annotated with Provider) – Ajay Sep 18 '17 at 17:51

0 Answers0