Hackforge Academy

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;
}

๐Ÿš€ Learn Spring Boot with real-world projects

๐Ÿ’ก Build REST APIs step by step

๐Ÿง  Improve backend development skills

๐ŸŽฏ Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

๐Ÿ

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
โš›๏ธ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
โ˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community