Java · Advanced level

Advanced level · Module 5: REST APIs with Spring Boot · Lesson 1 of 4

Your First Spring Boot Service

Create a Spring Boot project, understand auto-configuration and starters, and return JSON from a controller.

In this lesson you will

  • Generate and run a Spring Boot project
  • Write a @RestController with GET endpoints returning records as JSON

Key concepts

Spring Boot starterauto-configuration@RestController@GetMapping and @PathVariable

The lesson

Spring Boot is a framework for building Java services quickly. Starters such as spring-boot-starter-web bring in compatible libraries, and auto-configuration sets up an embedded web server, JSON conversion and more based on what is on the classpath.

A @RestController class handles HTTP requests. @GetMapping("/events/{id}") maps a GET request, @PathVariable reads part of the URL and @RequestParam reads query parameters. Returning a record makes Spring convert it to JSON automatically.

Run the application from the IDE or with mvn spring-boot:run, then call the endpoints from a browser or curl. Configuration such as the port lives in application.properties.

Step by step

  1. Create a project with the web starter.
  2. Write a controller with GET /events returning a list of records.
  3. Add GET /events/{id} with a path variable.
  4. Change the port in application.properties and restart.

Practice activity

Create an events API that returns a hard-coded list of youth-centre events and a single event by ID.

Expected result: Both endpoints return correct JSON, and an unknown ID returns 404 (implemented in the next lesson if not yet done).

Check your understanding

  1. What does a Spring Boot starter provide?

    Show answer

    A tested set of dependencies for one purpose, such as building web applications.

  2. Which annotation reads a value from the URL path?

    Show answer

    @PathVariable.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.