Madeline Lobdell
Madeline Lobdell

Reputation: 11

Spring Boot Controller Not Registering and 404 Error on POST Request

I’m working on a Spring Boot application, and I’m having trouble getting my controller to register correctly. When I try to POST data to the /student/add endpoint, I receive a 404 error, indicating that the endpoint cannot be found.

My Controller:

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostConstruct
    public void init() {
        System.out.println("StudentController initialized!"); // Debug statement
    }

    @PostMapping("/add")
    public String add(@RequestBody Student student) {
        studentService.saveStudent(student);
        return "New user Added";
    }

    @GetMapping("/getAll")
    public List<Student> getAllStudents() {
        return studentService.getAllStudents();
    }
}

My Student Model:

@Entity
public class Student {

    @Id
    private String password;
    private String username;
    private String email;

    // Getters and setters
}

Project Setup: Project Setup

Ensured the controller is in the correct package (under the main application package). Added @RestController and @RequestMapping("/student") annotations to the controller. Verified dependencies for Spring Web and Spring Data JPA in pom.xml. Cleaned and rebuilt the project. Checked application logs for clues.

Upvotes: 1

Views: 75

Answers (3)

Haitam-Elgharras
Haitam-Elgharras

Reputation: 138

make sure your main application class (StudentSystemApplication.java) has component scanning enabled for the correct base package. It should look like this:

@SpringBootApplication
@ComponentScan(basePackages = "com.connectingfrontandback")
public class StudentSystemApplication {
 public static void main(String[] args) {
    SpringApplication.run(StudentSystemApplication.class, args);
 }
}

Because @SpringBootApplication includes component scanning functionality that automatically detects annotations like @RestController, @Service, @Repository, and @Component. When these annotations are found, Spring creates and manages those classes as beans. However, this scanning only works within the package containing your main class and its sub-packages. So if your main application class is in a different package than your components, you need to explicitly add @ComponentScan(basePackages = "com.connectingfrontandback") to ensure Spring finds and manages all your annotated classes correctly.

Upvotes: 0

Mar-Z
Mar-Z

Reputation: 4828

What does it mean main application package? This is the package where you have the main application class (StudentsystemApplication.java). In your case it is com.connectingfrontandback.studentsystem

Following the best practice in Spring Boot you should move all other packages to be under the above. Like this:

com.connectingfrontandback.studentsystem
com.connectingfrontandback.studentsystem.controller
com.connectingfrontandback.studentsystem.model
com.connectingfrontandback.studentsystem.repository
com.connectingfrontandback.studentsystem.service

Otherwise you would need to manage quite a few annotations manually.

Upvotes: 0

Babji Polisetti
Babji Polisetti

Reputation: 66

Your controller package should be under your base package. currently controller package is under java.com.connectingfrontandback.controller it should be java.com.connectingfrontandback.**studentsystem**.controller.

Alternatively can add component scan annotation on StudentsystemApplication.java to consider controller layer without refactoring package.

@ComponentScan(basePackages = "java.com.connectingfrontandback.controller")

Upvotes: 1

Related Questions