-1

Spring newbie here. Three classes

Employee.java

package com.example.model;

public class Employee {

private int employeeId;
private String name;
private String email; 

public Employee(int employeeId, String name, String email)
{
    this.setEmployeeId(employeeId);
    this.setName(name);
    this.setEmail(email); 
}

public int getEmployeeId() {
    return employeeId;
}

public void setEmployeeId(int employeeId) {
    this.employeeId = employeeId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

EmployeeDao.java

@Component
public class EmployeeDao {

static List<Employee> list = new ArrayList<>();
        
        static 
        {
            list.add(new Employee(1234,"Nancy", "nancy@mail.com"));
            list.add(new Employee(5678, "Daniel","daniel@mail.com"));
            list.add(new Employee(9101, "Scott", "scott@mail.com"));
        }

public List<Employee> getAllEmployees()
 {
    return list;
 }

}

EmployeeController.java

@RestController
public class EmployeeController {

@Autowired
EmployeeDao service;

@GetMapping(path = "/employees")    
public List<Employee> getAll()
 {
    System.out.println(service.getAllEmployees());
    return service.getAllEmployees();
 }
}

Postman execution of GET employees throws up this error(404) enter image description here

Project Setup

enter image description here

Raghu
  • 1,141
  • 5
  • 20
  • 39

3 Answers3

0

In a spring boot application there can be a property server.servlet.context-path defined which is the root path of the application.

Search in the application.properties for this property and if defined, add it to the url path that you call with Postman.

If this is not the case then you can use the following annotation in the class that has the main method that has the @SpringBootApplication

@ComponentScan({"com.example.service", "com.example.model"})
Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
0

As your main application class is under the package demo, with the @SpringBootApplication, it scans the demo package and all its children packages. Your controller is not scanned in this case. One of the solutions is to reorganise your packages. For example, remove the demo package.

Oceania Water
  • 267
  • 2
  • 11
-1

Did you put this @SpringBootApplication to your main class? Seems your controller is not scanned. https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-using-springbootapplication-annotation.html

Oceania Water
  • 267
  • 2
  • 11