GET /items
Hub › Java › Beginner › GET /items
Goal
Ship the tier exit artifact: a Spring Boot app with GET /items that returns three items as JSON. After this page you will have a buildable, runnable Maven project that answers a curl from a second terminal.
Prerequisites
What "done" looks like
A reader on another machine clones the project, runs one command, and curl http://localhost:8080/items returns three items as JSON. That is the bar.
We will:
- Rename
HelloControllertoItemControllerand change the path to/items. - Move the
Itemrecord into its own file so the controller is small. - Run the build, then run the app, then
curlit.
Code
Delete src/main/java/com/example/itemsapi/HelloController.java.
Create src/main/java/com/example/itemsapi/Item.java:
package com.example.itemsapi;
public record Item(int id, String name) {}Create src/main/java/com/example/itemsapi/ItemController.java:
package com.example.itemsapi;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ItemController {
@GetMapping("/items")
public List<Item> items() {
return List.of(
new Item(1, "notebook"),
new Item(2, "pen"),
new Item(3, "stapler")
);
}
}Leave ItemsApiApplication.java and pom.xml exactly as Spring Initializr generated them.
Build
From the project root:
./mvnw clean packageMaven downloads dependencies on first run, then compiles, runs the generated test, and packages the app into target/items-api-0.0.1-SNAPSHOT.jar. Expected tail:
[INFO] BUILD SUCCESSRun
Two ways. Either works.
The Maven plugin:
./mvnw spring-boot:runOr the packaged jar:
java -jar target/items-api-0.0.1-SNAPSHOT.jarEither prints the Spring banner and ends with:
Tomcat started on port 8080
Started ItemsApiApplication in 1.234 secondsVerify
In a second terminal:
curl http://localhost:8080/itemsOutput:
[{"id":1,"name":"notebook"},{"id":2,"name":"pen"},{"id":3,"name":"stapler"}]With headers:
curl -i http://localhost:8080/itemsHTTP/1.1 200
Content-Type: application/json
...
[{"id":1,"name":"notebook"},{"id":2,"name":"pen"},{"id":3,"name":"stapler"}]Stop the server with Ctrl-C. That is the exit artifact.
You finished a beginner tier. What's next?
Two paths from here.
- Go deeper on the same platform. The intermediate tier on this same site teaches you to ship a thing that persists, tests itself, and talks to the world. If you liked beginner, that's the natural next step.
- Pick up an adjacent platform. The table below routes you across platforms based on what you actually want to build.
| You just finished | Natural next platform | Why |
|---|---|---|
| iOS beginner | iOS intermediate, then Android beginner | Stay native, then learn the other mobile platform with a head start on the Compose/SwiftUI mental model. |
| Android beginner | Android intermediate, then Golang beginner | Backend-for-frontend pairs naturally with a mobile client. |
| Golang beginner | AWS beginner, then Golang intermediate | Deploy your endpoint before adding persistence/tests. |
| Java beginner | Java intermediate, then AWS beginner | JVM persistence + validation first, then deploy. |
| AWS beginner | Golang beginner | Have a backend to deploy. AWS without a service to host is reference, not curriculum. |
Or jump back to the Hub and pick a different goal.