Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using different packages for Controllers when using Spring

If I create a standard Spring project in the spring toolkit it works great!

I visit localhost:8080/greeting and get my hello world response.

If I copy those 2 files into another package in my source tree and then visit localhost:8080/greeting I get a :

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Oct 17 18:15:45 BST 2014
There was an unexpected error (type=Not Found, status=404).

Moving the 3 classes into the same default package fixes the problem but from a source tree organization perspective, this is not what I want to do.

I expect this to be due to some auto-configuration so please can somebody tell me what I have to do to allow my project to support multiple packages as controllers and objects.

like image 396
RenegadeAndy Avatar asked Oct 17 '14 17:10

RenegadeAndy


1 Answers

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.

like image 162
Shirantha Madusanka Avatar answered Nov 15 '22 16:11

Shirantha Madusanka