If you want a small REST API that actually works and does not explode on first request you are in the right place. This playful lab walks you from an empty Spring Boot project to a testable RESTful API with persistence and minimal suffering. Keywords you will see along the way include Spring Boot, Spring MVC, JPA, Hibernate, H2 database, Postman and API testing.
A simple resource API that can list items fetch a single item create new items and delete items. The API uses Spring Boot for wiring Spring MVC for request handling JPA and Hibernate for persistence and H2 as an in memory database for fast feedback. Tests use MockMvc or TestRestTemplate so you do not ship regressions to production by accident.
Create the project with Spring Initializr or a Maven archetype and include these starters
spring-boot-starter-web
for controllers and JSON handlingspring-boot-starter-data-jpa
for JpaRepository supportcom.h2database h2
for an in memory database during the labspring-boot-starter-test
for testing helpersDefine a simple domain class Item with fields like id name and description. Annotate id with @Id and @GeneratedValue and add JPA annotations as needed. Keep domain objects focused on persistence and not on shaping API payloads.
Create a repository interface
public interface ItemRepository extends JpaRepository- { }
This gives you CRUD methods without writing SQL or pretending to enjoy boilerplate.
Implement a controller annotated with @RestController and map endpoints under /api/items. Use ResponseEntity for predictable HTTP codes and validation annotations like @Valid and @NotBlank for input checks.
@RestController
@RequestMapping("/api/items")
public class ItemController {
private final ItemRepository repo;
public ItemController(ItemRepository repo) {
this.repo = repo;
}
@GetMapping
public ResponseEntity> list() {
return ResponseEntity.ok(repo.findAll());
}
@GetMapping("/{id}")
public ResponseEntity- get(@PathVariable Long id) {
return repo.findById(id)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
@PostMapping
public ResponseEntity
- create(@Valid @RequestBody Item item) {
Item saved = repo.save(item);
return ResponseEntity.status(HttpStatus.CREATED).body(saved);
}
@DeleteMapping("/{id}")
public ResponseEntity
delete(@PathVariable Long id) {
repo.deleteById(id);
return ResponseEntity.noContent().build();
}
}
Configure application.properties to point to H2 and to enable the console for quick inspection. Example properties look like this
spring.datasource.url=jdbc:h2:mem:labdb
spring.datasource.driver-class-name=org.h2.Driver
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update
Use CommandLineRunner or SQL data files to seed the database so you can stop manually creating data with a hammer and a curl request.
Write integration tests with MockMvc or TestRestTemplate to exercise your endpoints and to catch regressions before your users do. Use Postman or curl when you want manual verification or to flex on your teammates.
Keep controller methods focused on request handling. Move business rules into service classes when logic grows. Design DTOs to separate API contracts from internal domain classes. That avoids accidental data leaks and makes versioning less painful.
Follow these patterns faithfully and you will leave the lab with a small production ready pattern and fewer hair loss incidents.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.