Search code examples
springrestspring-bootspring-mvcversioning

WebMvcConfigurationSupport produce errors in SpringBoot


I am doing versioning of my rest api. I found a nice way to do it here :

https://github.com/augusto/restVersioning

@RestController
@RequestMapping("/user")
@VersionedResource(media = Version.MEDIA)
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("")
    @VersionedResource(from = "1.0", to = "1.9")
    public String getUser() {
        return "user v1";
    }

    @GetMapping("")
    @VersionedResource(from = "2.0")
    public String getUserV2() {
        return "user v2";
    }

It works well, but to do it needs a class that extends WebMvcConfigurationSupport:

@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {
    @Autowired
    private ContentNegotiationManager contentNegotiationManager;

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        VersionRequestMappingHandlerMapping handlerMapping = new VersionRequestMappingHandlerMapping();
        handlerMapping.setOrder(0);
        handlerMapping.setRemoveSemicolonContent(false);
        handlerMapping.setContentNegotiationManager(contentNegotiationManager);

        return handlerMapping;
    }
}

And this produce many strange errors, like new LazyInitializationException, changing date format, bug with uri arguments... Even if I leave this class empty, it will produce bugs and versioning will be disable. When I comment this extends WebMvcConfigurationSupport all works well again, except that versoning is disable, of course. So I'm sure that the error come from here.

So the question is, why WebMvcConfigurationSupport produce errors, and which solutions do I have? Thank you


Solution

  • Solution was to use WebMvcRegistrations instead of WebMvcConfigurationSupport because it disable autoconfiguration.

     @Configuration
    public class WebConfiguration implements WebMvcRegistrations {
    
        @Autowired
        private ContentNegotiationManager contentNegotiationManager;
    
        @Override
        public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
            VersionRequestMappingHandlerMapping handlerMapping = new VersionRequestMappingHandlerMapping();
            handlerMapping.setOrder(0);
            handlerMapping.setRemoveSemicolonContent(false);
            handlerMapping.setContentNegotiationManager(contentNegotiationManager);
    
            return handlerMapping;
    
        }
    }