Using different packages for Controllers when using Spring

Moving the Springbootapplication(application.java) file to another package resolved the issue for me. Keep it separate from the controllers and repositories. You can use any number of packages and have multiple controllers.But use@ComponenScan(basePackages={" "," "}) and mention all the packages.


If your code looks like in this tutorial, then Open servlet-context.xmland change

<context:component-scan base-package="..."/>

You can list multiple packages. Use comma as separator.

Update:

That was for XML-based configuration.

If you create "Spring Boot App", then configuration is annotation-based. In this case you need to search for classes annotated with @ComponentScan. Typically it is a main class or class called AppConfig.

By default it searches recursively controllers and other components starting from the class' package. You can add parameters to this annotation like this:

@ComponentScan({ "x.y.z.services", "x.y.z.controllers" })

Assuming the main method is in the package called com.setech.app and a controller is in a package called com.setech.controller.

For spring-boot 1.3.x upwards try this by adding "scanBasePackages" like this.

@SpringBootApplication(scanBasePackages = { "com.setech"} )
public class ResttanslatorApplication {

    public static void main(String[] args) {

        SpringApplication.run(ResttanslatorApplication.class, args);
    }
}

Credit goes to Kamil Wozniak from here.