Build your first REST API in under 30 minutes
July 10, 2025
10 min read
Spring Boot is the most popular Java framework for building production-ready applications fast. With Spring Boot 3, you get native support for Java 17+, improved observability, and a cleaner architecture. Let's build a REST API from scratch.
Before we start, make sure you have:
The easiest way is using Spring Initializr. Go to start.spring.io, select:
Click Generate and extract the ZIP. Your project structure will look like this:
textmy-app/ ├── src/ │ ├── main/ │ │ ├── java/com/example/myapp/ │ │ │ └── MyAppApplication.java │ │ └── resources/ │ │ └── application.properties │ └── test/ └── pom.xml
A controller handles HTTP requests. Let's create a simple one:
java@RestController @RequestMapping("/api/greet") public class GreetController { @GetMapping public String greet() { return "Hello from Spring Boot 3!"; } @GetMapping("/{name}") public String greetUser(@PathVariable String name) { return "Hello, " + name + "!"; } }
@RestController combines @Controller and @ResponseBody — it tells Spring this class handles REST requests and returns JSON/text directly.
Let's create a Product entity with JPA:
java@Entity public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private double price; // Constructors, Getters & Setters public Product() {} public Product(String name, double price) { this.name = name; this.price = price; } // getters and setters... }
Now create the repository — Spring Data JPA gives you CRUD for free:
javapublic interface ProductRepository extends JpaRepository<Product, Long> { // That's it! Spring generates findAll, findById, save, delete automatically List<Product> findByName(String name); // custom query }
Wire up the service layer and controller:
java@Service public class ProductService { private final ProductRepository repo; public ProductService(ProductRepository repo) { this.repo = repo; } public List<Product> getAll() { return repo.findAll(); } public Product getById(Long id) { return repo.findById(id).orElseThrow(); } public Product create(Product p) { return repo.save(p); } public void delete(Long id) { repo.deleteById(id); } } @RestController @RequestMapping("/api/products") public class ProductController { private final ProductService service; public ProductController(ProductService service) { this.service = service; } @GetMapping public List<Product> getAll() { return service.getAll(); } @GetMapping("/{id}") public Product getById(@PathVariable Long id) { return service.getById(id); } @PostMapping @ResponseStatus(HttpStatus.CREATED) public Product create(@RequestBody Product product) { return service.create(product); } @DeleteMapping("/{id}") @ResponseStatus(HttpStatus.NO_CONTENT) public void delete(@PathVariable Long id) { service.delete(id); } }
properties# H2 in-memory database (great for development) spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.jpa.hibernate.ddl-auto=update spring.h2.console.enabled=true server.port=8080
Run the app with Maven:
bashmvn spring-boot:run
Test your endpoints with curl or Postman:
bash# Get all products curl http://localhost:8080/api/products # Create a product curl -X POST http://localhost:8080/api/products \ -H "Content-Type: application/json" \ -d '{"name":"Laptop","price":999.99}'
Visit http://localhost:8080/h2-console to browse your in-memory database during development. Use JDBC URL: jdbc:h2:mem:testdb
What's Next?