Spring Web: ControllerAdvice

159 Views Asked by At

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)?

0

There are 0 best solutions below