Important Methods of HTTP
The main methods of HTTP we build web services for are:
- GET: Reads an existing data.
- PUT: Updates existing data.
- POST: Creates new data.
- DELETE: Deletes the data.
1. GET
The default request method for HTTP. We don’t have any request body with this method, but we can define multiple request parameters or path variables in the URL. This method is used for getting obtaining some resources. Depending on the presence of an ID parameter, either we can fetch a specific resource or fetch a collection of resources in the absence of the parameter. Sample GET request in Spring Boot Controller:
@GetMapping("/user/{userId}")
public ResponseEntity<Object> getUser(@PathVariable int userId) {
UserEntity user = userService.getUser(userId);
return new ResponseEntity<>(user, HttpStatus.OK);
}
Example of GET operation to perform in an application:
- GET/employees: This will retrieve all employee details.
2. POST
The POST method of HTTP is used to create a resource. We have a request body in this method and can also define multiple request parameters or path variables in the URL. Sample POST request in Spring Boot Controller:
@PostMapping(value = "/user")
public ResponseEntity<Object> addUser(@RequestBody UserEntity user) {
userService.saveOrUpdate(user);
return new ResponseEntity<>("User is created successfully", HttpStatus.CREATED);
}
Example of POST operation to perform in an application:
- POST/employees: This will create an employee.
3. PUT
The PUT method of HTTP is used to update an existing resource. We have a request body in this method and can also define multiple request parameters or path variables in the URL. Sample PUT request in Spring Boot Controller:
@PutMapping("/user/{userId}")
public ResponseEntity<Object> getUser(@RequestBody UserEntity user) {
userService.saveOrUpdate(user);
return new ResponseEntity<>("User is updated successfully", HttpStatus.OK);
}
Example of PUT operation to perform in an application:
- PUT/employees/{id}: This will update an existing employee’s details.
4. DELETE
The DELETE method of HTTP is used to remove a resource. We don’t have a request body in this method but can define multiple request parameters or path variables in the URL. We can delete multiple or single records, usually based on whether we have an ID parameter or not. We can delete multiple or single records, usually based on whether we have an ID parameter or not. Sample DELETE request in Spring Boot Controller:
@DeleteMapping(value = "/user")
public ResponseEntity<Object> addUser(@PathVariable int userId) {
userService.deleteUser(userId);
return new ResponseEntity<>("User is deleted successfully", HttpStatus.OK);
}
Example of DELETE operation to perform in an application:
- DELETE/employees: This will delete all employees.
REST web services use the Status-Line part of an HTTP response message to inform clients of their request’s ultimate result.
Spring Boot – Introduction to RESTful Web Services
RESTful Web Services REST stands for REpresentational State Transfer. It was developed by Roy Thomas Fielding, one of the principal authors of the web protocol HTTP. Consequently, REST was an architectural approach designed to make the optimum use of HTTP protocol. It uses the concepts and verbs already present in HTTP to develop web services. This made REST incredibly easy to use and consume, so much so that it is the go-to standard for building web services today.
A resource can be anything, it can be accessed through a URI (Uniform Resource Identifier). Unlike SOAP, REST does not have a standard messaging format. We can build REST web services using many representations, including both XML and JSON, although JSON is the more popular option. An important thing to consider is that REST is not a standard but a style whose purpose is to constrain our architecture to a client-server architecture and is designed to use stateless communication protocols like HTTP.