1

I have the simple class to catch some exceptions like this:

@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(RuntimeException.class)
    public ResponseEntity<Object> handleRuntimeException(Exception ex) {
        log.debug("[RuntimeException throw]");
        return new ResponseEntity<>(responseMessage, HttpStatus.OK);
    }
}

Then I can throw new RuntimeException and can see the output log in console.

It well done works but I have to move the RestResponseEntityExceptionHandler.class to another project as a lib and add this like a maven dependency in pom.xml.

Also in the main project added ControllerAdvice which extends moved:

@ControllerAdvice
public class ResponseEntityExceptionHandler extends RestResponseEntityExceptionHandler { }

After that it does not work. I have the next exception:

org.springframework.context.NoSuchMessageException: No message found under code 'description' for locale 'ru_RU'.

When RestResponseEntityExceptionHandler was in the main project, I had the next bean definition:

@Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource
                = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

and in the 'resource' folder I had messager_ru.properties file. I moved the messageSource bean config and properties file too.

Perhaps the point is that the project that I moved all this to is a library (no main function)?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
MrLebovsky
  • 88
  • 9
  • Are you missing the appropriate @ComponentScan? Can you tell use the package for RestResponseEntityExceptionHandler and shows us your Spring configuration? – Simon Dec 09 '20 at 13:52
  • @Simon I supplemented the question. Thank you for your interest! How can I tell use the package for RestResponseEntityExceptionHandler? – MrLebovsky Dec 09 '20 at 13:59
  • You need to make sure to keep the scope of your post well-defined. Is this question about getting the `@ControllerAdvice` to work (because it seems you have got it to work)? The `NoSuchMessageException` seems quite unrelated to `@ControllerAdvice` (so should be treated in a separate SO post) – Simon Dec 09 '20 at 14:06

0 Answers0