0% found this document useful (0 votes)
10 views3 pages

RESTful Web Services

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

RESTful Web Services

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

RESTful Web Services

RESTful web services allow applications to communicate over the internet using the HTTP protocol. They
follow the principles of REST (Representational State Transfer), which emphasize stateless interactions and
the use of standard HTTP methods.

Stateless: In the context of RESTful web services, "stateless" means that each HTTP request from a client
to the server must contain all the information the server needs to understand and process the request. The
server does not store any information about the client's state between requests. This principle of statelessness
is one of the key constraints of the REST architecture.

Key Concepts

1. REST Controller
o Definition: A REST controller is a specialized controller in the Spring framework designed
to handle RESTful web services.
o Annotation: @RestController.
o Functionality: It simplifies the development of RESTful APIs by combining the
functionality of @Controller and @ResponseBody.
 @Controller: Used to define a web controller in Spring.
 @ResponseBody: Indicates that the return value of a method should be used as the
response body in HTTP responses.

@RestController
public class MyRestController {
// Methods will go here
}

2. Request Mapping
o Definition: Request mapping is used to map HTTP requests to specific handler methods of
REST controllers.
o Annotation: @RequestMapping, as well as more specific annotations like @GetMapping,
@PostMapping, @PutMapping, and @DeleteMapping.
o Functionality: It helps in directing incoming HTTP requests to the appropriate methods
based on the URL pattern and HTTP method.

@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}

3. Request Body
o Definition: The request body contains the data sent by the client in an HTTP request.
o Annotation: @RequestBody.
o Functionality: It binds the HTTP request body to a method parameter, allowing you to work
with the data sent by the client.

@PostMapping("/create")
public void createUser(@RequestBody User user) {
// Code to create user
}
4. Path Variable
o Definition: Path variables are used to extract values from the URI itself.
o Annotation: @PathVariable.
o Functionality: It allows you to dynamically capture and use values embedded in the URL
path.

@GetMapping("/users/{id}")
public User getUserById(@PathVariable("id") Long id) {
// Code to get user by ID
}

5. Request Parameter
o Definition: Request parameters are used to extract query parameters from the URI.
o Annotation: @RequestParam.
o Functionality: It allows you to retrieve specific parameters passed in the query string of the
URL.

@GetMapping("/search")
public List<User> searchUsers(@RequestParam("name") String name) {
// Code to search users by name
}

HTTP Methods

1. GET API
o Definition: The GET method is used to retrieve resources from the server.
o Use Case: For read-only operations, such as fetching data.
o Example:

@GetMapping("/users")
public List<User> getAllUsers() {
// Code to return all users
}

2. POST API
o Definition: The POST method is used to create new resources on the server.
o Use Case: For creating a new entry or resource.
o Example:

@PostMapping("/users")
public User createUser(@RequestBody User user) {
// Code to create a new user
}

3. PUT API
o Definition: The PUT method is used to update existing resources on the server.
o Use Case: For updating an existing entry or resource.
o Example:

@PutMapping("/users/{id}")
public User updateUser(@PathVariable("id") Long id, @RequestBody User user) {
// Code to update user with given ID
}
4. DELETE API
o Definition: The DELETE method is used to delete resources from the server.
o Use Case: For removing an entry or resource.
o Example:

@DeleteMapping("/users/{id}")
public void deleteUser(@PathVariable("id") Long id) {
// Code to delete user with given ID
}

Example Application

Here's a simple example of a RESTful web service for managing users.

@RestController
@RequestMapping("/api/users")
public class UserController {

// Get all users


@GetMapping
public List<User> getAllUsers() {
// Code to return all users
}

// Get user by ID
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
// Code to return user by ID
}

// Create a new user


@PostMapping
public User createUser(@RequestBody User user) {
// Code to create a new user
}

// Update an existing user


@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User user) {
// Code to update user by ID
}

// Delete a user
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
// Code to delete user by ID
}
}

Summary

 @RestController: Simplifies controller setup for RESTful services.


 Request Mapping: Maps HTTP requests to controller methods using annotations.
 HTTP Methods: GET, POST, PUT, DELETE for CRUD operations.
 @RequestBody: Binds HTTP request body to method parameters.
 @PathVariable: Extracts values from the URI.
 @RequestParam: Extracts query parameters from the URI.

You might also like