How to Create REST API in Spring Boot: Simple Guide
To create a REST API in Spring Boot, define a class annotated with
@RestController and map HTTP requests using @GetMapping, @PostMapping, etc. Use Spring Boot's embedded server to run your application and handle requests easily.Syntax
Here is the basic syntax to create a REST API endpoint in Spring Boot:
@RestController: Marks the class as a REST controller to handle HTTP requests.@RequestMappingor specific method mappings like@GetMapping,@PostMapping: Define the URL path and HTTP method.- Method returns data (usually JSON) directly to the client.
java
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping("/hello") public String sayHello() { return "Hello, World!"; } }
Example
This example shows a complete Spring Boot application with a REST API endpoint that returns a greeting message when accessed via HTTP GET at /hello.
java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class RestApiApplication { public static void main(String[] args) { SpringApplication.run(RestApiApplication.class, args); } @RestController static class GreetingController { @GetMapping("/hello") public String greet() { return "Hello, World!"; } } }
Output
When you run the application and visit http://localhost:8080/hello in a browser or HTTP client, it returns:
Hello, World!
Common Pitfalls
- Forgetting
@RestControllercauses Spring to treat the class as a regular controller, which may try to resolve views instead of returning data. - Not specifying the correct HTTP method annotation like
@GetMappingor@PostMappingcan lead to 404 errors. - Missing
@SpringBootApplicationon the main class prevents the app from starting properly. - Returning complex objects without proper getters or without
@ResponseBody(if not using@RestController) can cause serialization issues.
java
/* Wrong way: Missing @RestController, returns view name instead of data */ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class WrongController { @GetMapping("/wrong") public String wrong() { return "Hello"; // Spring looks for a view named 'Hello' } } /* Right way: Use @RestController to return data directly */ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class RightController { @GetMapping("/right") public String right() { return "Hello"; // Returns string as response body } }
Quick Reference
Here are quick tips for creating REST APIs in Spring Boot:
- Use
@RestControllerfor classes handling REST requests. - Map URLs with
@GetMapping,@PostMapping,@PutMapping,@DeleteMapping. - Return Java objects or strings; Spring Boot auto-converts to JSON.
- Run your app with
SpringApplication.run()in the main class annotated with@SpringBootApplication. - Test endpoints with browser or tools like Postman or curl.
Key Takeaways
Annotate your class with @RestController to create REST endpoints.
Use @GetMapping, @PostMapping, etc., to map HTTP methods and URLs.
Return data directly from methods; Spring Boot handles JSON conversion.
Ensure your main class has @SpringBootApplication and runs SpringApplication.run().
Test your API endpoints with a browser or HTTP client tools.