Spring @GetMapping example shows how to use @GetMapping annotation to map HTTP GET requests onto specific handler methods.
@GetMapping Overview
@GetMapping annotation maps HTTP GET requests onto specific handler methods. It is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).@GetMapping – shortcut for @RequestMapping(method = RequestMethod.GET)@GetMapping Example@GetMapping annotation for mapping HTTP GET requests onto specific handler methods.In the below example, the @GetMapping annotation mapping HTTP GET “/employees” and “/employees/{id}” requests onto specific handler methods getAllEmployees and getEmployeeById:
@GetMapping("/employees")
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
@GetMapping("/employees/{id}")
public ResponseEntity<Employee> getEmployeeById(@PathVariable(value = "id") Long employeeId)
throws ResourceNotFoundException {
Employee employee = employeeRepository.findById(employeeId)
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
return ResponseEntity.ok().body(employee);
}