Category: spring_boot
REST API in spring boot
Published on 16 Jun 2026
Explanation
Spring Boot
is a web service that allows
clients to communicate with a server
using HTTP methods such as GET,
POST, PUT, and DELETE. Spring Boot
simplifies REST API development
using annotations
like @RestController, @GetMapping,
@PostMapping, @PutMapping, and
@DeleteMapping.
Code:
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
Explanation
GET API is used to retrieve
data from the server.
Code:
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public String getUser(
@PathVariable Long id) {
return "User ID: " + id;
}
}
Explanation
POST API is used to create
new resources on the server.
Code:
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping
public String createUser(
@RequestBody User user) {
return "User created: " +
user.getName();
}
}
Explanation
PUT API is used to update
an existing resource.
Code:
@RestController
@RequestMapping("/users")
public class UserController {
@PutMapping("/{id}")
public String updateUser(
@PathVariable Long id, @RequestBody
User user) {
return "User updated: " + id;
}
}
Explanation
DELETE API is used to remove
a resource from the server.
Code:
@RestController
@RequestMapping("/users")
public class UserController {
@DeleteMapping("/{id}")
public String deleteUser(
@PathVariable Long id) {
return "User deleted: " + id;
}
}
Explanation
A typical REST API returns JSON
responses instead of plain text.
Code:
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUser(
@PathVariable Long id) {
return new User(id, "John");
}
}
class User {
private Long id;
private String name;
}