Posts

Showing posts with the label Spring Web Annotations

Spring @RequestMapping Annotation Example

Image
In this section we will learn about @RequestMapping  Annotation.   @RequestMapping  is used to map web requests onto specific handler classes and/or handler methods.  @RequestMapping  can be applied to the controller class as well as methods. It has the following optional options name : Assign a name to this mapping. value :  The primary mapping expressed by this annotation. method : The HTTP request methods to map to headers : The headers of the mapped request, narrowing the primary mapping. @Controller @RequestMapping ( "/employees" ) public class EmployeeController { @RequestMapping ( "/employee" ) public String getEmployee ( ) { } } A  @RequestMapping  on the class level is not required. Without it, all paths are simply absolute, and not relative. This means if you specify the class level annotations, the URL shall be relative, it shall be  http://localhost:8080/employees/employee (URL to Handler mapping) and likewise. @Controller public

Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, and @PatchMapping Annotations Example

Image
In this section we will learn about  @GetMapping , @PostMapping , @PutMapping , @DeleteMapping , and @PatchMapping  Annotations.   @GetMapping @GetMapping  annotation for mapping HTTP GET requests onto specific handler methods. Specifically,  @GetMapping  is a composed annotation that acts as a shortcut for  @RequestMapping(method = RequestMethod.GET) . Example: @GetMapping ( " /users " ) public List< User > getAllusers() { return userRepository . findAll(); } @PostMapping @PostMapping a nnotation for mapping HTTP POST requests onto specific handler methods. Specifically,  @PostMapping  is a composed annotation that acts as a shortcut for  @RequestMapping(method = RequestMethod.POST) . Example: @PostMapping ( " /users " ) public User createUser( @RequestBody User user) { return userRepository . save(user); } @PutMapping @PutMapping  annotation for mapping HTTP PUT requests onto specific handler methods. Specifically,  @PutMapping  is a composed annotat