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", "[email protected]"));
list.add(new Employee(5678, "Daniel","[email protected]"));
list.add(new Employee(9101, "Scott", "[email protected]"));
}
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)

Project Setup
CodePudding user response:
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"})
CodePudding user response:
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.
CodePudding user response:
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
CodePudding user response:
You need to also add @RequestMapping
@RequestMapping is a class level
@GetMapping is a method-level
Here is an example:
@Slf4j
@Controller
@RequestMapping("/employee")/* The @Request-Mapping annotation, when applied
at the class level, specifies the kind of requests
that this controller handles*/
public class EmployeeController {
@GetMapping("/employees")/*@GetMapping paired with the classlevel
@RequestMapping, specifies that when an
HTTP GET request is received for /order,
getAllEmployees() will be called to handle the request..*/
public List<Employee> getAll(){
System.out.println(service.getAllEmployees());
return service.getAllEmployees();
}
}
also, for debugging, you can include spring boot actuator starter spring-boot-starter-actuator to see all mapping that are loaded as part of your app. see https://www.baeldung.com/spring-boot-actuators
/actuator/mappings

