Reputation: 23
After i write a simple route using rest controller and spring boot the routes always return 404 whether If the route is mention in a separate package or in same package. The route only works if the route is defined in the main function where @SpringBootApplication is mentioned please help me package com.gowtham;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@ComponentScan(basePackages = "com.gowtham")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RestController
public class controller {
@GetMapping("/greet")
public String hello() {
return "HELLO";
}
}
The /greet route works
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ccc {
@GetMapping("/gg")
public String hello() {
return "HELLO";
}
}
But this route /gg which is inside .controller package which is under the main package doesn't work i tried moving the class "ccc" inside the main package and i also tried to scan base packages but no use
if i import the same program in other computers it is running fine
Package Structure
Upvotes: 2
Views: 3167
Reputation: 16
I'm also facing the same issue as mentioned above. I tried using Spring Boot version 2.7.9 instead of 3.0.3. It worked fine, although not sure what changed in the new version.
Upvotes: 0
Reputation: 165
Make sure that your Spring Boot application is scanning the correct packages for your controllers. You can specify the base package for component scanning by adding the @ComponentScan annotation to your main class.
@SpringBootApplication
@ComponentScan(basePackages = "com.gowtham")
public class DemoApplication {
// ...
}
Upvotes: 0