3

This is WebConfig code I am working on:

package hello.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/greeting").setViewName("greeting");
    }
}

And this is my Application.class

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication
public class Application extends SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}

It seems to be a spring-boot issue that these class methods don't get called in some systems. The corresponding issue is reported at: https://github.com/spring-projects/spring-boot/issues/2870

My question is, can we map resources mapped in this class, outside this class as a temporary workaround?

If yes, how do we do this?

Update: Following Andy Wilkinson's suggestion I removed @EnableWebMvc and the demo app started working. Then I tried stripping down project files one by one to see at what point error disappears. I found that I had two classes in the project, one, extended from WebMvcConfigurationSupport and the second, from WebMvcConfigurerAdapter. removing the former class from the project fixed the error.

What I want to know is, why did this happen? Secondly, why doesnt this error appear on all systems?

ask5332
  • 33
  • 1
  • 1
  • 4
  • If you are using Spring Boot you don't need `@EnableWebMvc` as that is already automatically done by Spring Boot. Is the bean constructed? Is it being picked up by your component-scan? – M. Deinum Jun 18 '15 at 09:16
  • Yes bean is getting constructed. I forgot to mention one thing. When I rename WebConfig to MvcConfig, everything starts working. But this appears to be system specific workaround... It doesn't work for all systems. – ask5332 Jun 18 '15 at 10:28
  • Please add your application class. And have you tried with removing the `@EnableWebMvc` as that isn't needed in a Spring Boot application. – M. Deinum Jun 18 '15 at 10:31
  • Ok.. I will add the application class. Yes I tried removing '@EnableWebMvc'...Doesn't still work – ask5332 Jun 18 '15 at 11:18
  • In which case it isn't working? One thing that is wrong is the fact that you use `SpringApplication` as the source, whereas it should be `Application`. – M. Deinum Jun 18 '15 at 11:46
  • I added Application.java class – ask5332 Jun 18 '15 at 11:46
  • I changed that and still no change. Issue is that this same piece of code works on some systems and it doesnt work on others. All systems that i tested had ubuntu 14.04, eclipse luna and oracle java 8 – ask5332 Jun 18 '15 at 13:18
  • Are you deploying this application or are you using the `main` method to run it? – M. Deinum Jun 18 '15 at 13:26
  • Start with --debug and get the information on what matched. I suspect that there is a part of the Spring MVC auto config isn't properly detected. Either due to not being available or rights or something. Spring MVC is what is calling this, if that isn't available nothing will be called. – M. Deinum Jun 18 '15 at 13:43

1 Answers1

8

The problem is that WebConfig is in the config package and Application is in the hello package. @SpringBootApplication on Application enables component scanning for the package in which it's declared and that package's sub-packages. In this case that means that hello is the base package for component scanning and, therefore, WebConfig in the config package is never found.

To solve the problem I'd move WebConfig into the hello package or a sub-package, for example hello.config.

Your latest update on GitHub changed WebConfig from extending WebMvcConfigurerAdapter to extending WebMvcConfigurationSupport. WebMvcConfigurationSupport is the class that is imported by @EnableWebMvc so annotating your class with @EnableWebMvc and extending WebMvcConfigurationSupport will be configuring things twice. You should go back to extending WebMvcConfigurerAdapter as you were before.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • You should really update the code in the question as well, otherwise this is going to get very confusing. – Andy Wilkinson Jun 18 '15 at 14:49
  • @AndyWilkinson thanks for pointing that out, I felt like the answer didn't match up with the question any more. I can recommend checking the question's version history if anyone else stumbles upon this, to see what the code looked like originally. – melwil Sep 12 '16 at 10:37