I wanted to create an annotation on for my rest api Controllers. To do that, I created a class which extends WebMvcConfigurationSupport to override the requestMappingHandlerMapping bean.

All worked as I expected, until I notice some endpoint doesn't work anymore, and catch exception LazyInitializationException. Note : If I had @Transactionnal on my class, it still doesn't work. Some endpoints respond with wrong date format, and lot of strange things like this.

If I delete the class which extends WebMvcConfigurationSupport, everything works again. If a clear everything in this class, but keep the extends WebMvcConfigurationSupport, strange problems come back. So the problem come from WebMvcConfigurationSupport. This class is annotated only with @Configuration.

Comment From: wilkinsona

Sub-classing WebMvcConfigurationSupport (or using @EnableWebMvc) completely disables Spring Boot's auto-configuration of Spring MVC. You haven't said why you want to replace the request mapping handler mapping bean, but you may want to consider using a WebMvcConfigurer bean to customise the auto-configuration rather than replacing it completely.

If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

Comment From: julian-christl

I found this during research for a similar, if not the same, problem and could solve it. Therefore, I'd like to add the solution for anyone else coming across this issue.

This bean is all you need:

@Bean
public WebMvcRegistrations webMvcRegistrations() {
    return new WebMvcRegistrations() {
        @Override
        public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
            // Return here your custom mapper
            return new RequestMappingHandlerMapping;
        }
    };
}

WebMvcAutoConfiguration checks in createRequestMappingHandlerMapping() whether a mapper has been defined in a WebMvcRegistrations or not.

The same principle works for RequestMappingHandlerAdapter and ExceptionHandlerExceptionResolver.

All this doesn't reactive the auto-configuration.