Spring Boot FAQ: Top Questions
5. How do you create a REST API using Spring Boot?
Creating REST APIs with Spring Boot is straightforward using annotations like @RestController
, @GetMapping
, @PostMapping
, etc.
πΊοΈ Step-by-Step Instructions:
- Create a class and annotate it with
@RestController
. - Add handler methods using request mapping annotations.
- Start the application and access endpoints via browser or tools like Postman.
π₯ Example Input:
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}
π Expected Output:
Accessing /hello returns "Hello, Spring Boot!"
π οΈ Use Cases:
- Creating microservices.
- Exposing data to front-end apps.
- Handling HTTP requests and responses in backend services.