'scanBasePackages on SpringBootApplication breaks endpoints

I am using an external dependency in my SpringBootApplication.

The main class is defined as

@ConfigurationPropertiesScan
@EnableConfigurationProperties
@SpringBootApplication(scanBasePackages = {"com.companyA.app"})
    public class ApidemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(ApidemoApplication.class, args);
        }
    }

The client class is defined as

@Configuration
@ConfigurationProperties(prefix = "companyA")
@EnableConfigurationProperties
public class MyClient {
      @Autowired
      @ConstructorBinding
      public MyClient(ApiClient apiClient){
             this.apiClient = apiClient);
      }
}

When I use the scanBasePackages annotation, all my existing endpoints stop working, giving a 404 not found

{
    "timestamp": "2022-02-02T00:33:43.210+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/endpoint1"
}

If I do not use the scanBasePackages annotation, the compile fails with the error:

MyClient is annotated with @ConstructorBinding but it is defined as a regular bean which caused dependency injection to fail.

Action:

Update your configuration so that MyClient is defined via @ConfigurationPropertiesScan or @EnableConfigurationProperties.

Why is scanBasePackages breaking all my endpoints?



Solution 1:[1]

I had the same problem, If it helps other people in the future.

Make sure you are also scanning the packages that contain your endpoints.

For example, if you have packages com.companyA.lib and com.companyA.app where lib contains some shared code and app has your endpoints.

Instead of scanning only classes in lib:

@SpringBootApplication(scanBasePackages = {"com.companyA.lib"})

Make sure you are scanning both packages:

@SpringBootApplication(scanBasePackages = {"com.companyA.lib", "com.companyA.app"})

this way SpringBoot will scan the dependencies in both packages.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tolkiana