Spring Boot services · Chapter 2 of 41

Your first Spring Boot app

By the end of this chapter you can start a Spring Boot app and answer HTTP requests with text and JSON, using values from the path and the query string. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Spring foundations
  • 5 tasks
  • Spring Boot
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

Why you need this

Ravi wants the menu in one place: "When the price of cutting chai changes, I don't want to walk to fifty counters." The counters, the Tapri app and the partner cafés will all ask one backend, Tapri Cloud, over HTTP.

With the Java you know, you would open a ServerSocket, read lines like GET /menu HTTP/1.1 yourself, split the path, turn "30" into a number, write the headers and build the JSON string by hand, and then do it again, safely, for many requests at once. Every new URL is more of that plumbing.

The lesson

The application class

main calls SpringApplication.run(TapriApplication.class, args). That starts the application context, the container that creates and wires your objects (Spring calls them beans), then an embedded Tomcat server.

The class you pass in decides what gets set up. Spring Boot's application annotation is three annotations in one:

  • @SpringBootConfiguration: this class is a source of configuration.
  • @EnableAutoConfiguration: look at the libraries on the classpath and set up sensible defaults (Spring MVC is there, so make a web server).
  • @ComponentScan: find your classes marked as components, such as a @RestController, in this package and below. That is why every file in this lab starts with package tapri;.

Your starter's TapriApplication has none of them, so run-app stops:

***************************
APPLICATION FAILED TO START
***************************

Description:

Web application could not be started as there was no org.springframework.boot.web.server.servlet.ServletWebServerFactory bean defined in the context.

No auto-configuration means no web server bean. Press Next to see what a working start does:

When it works, the log says so:

2026-10-06T22:53:26.188Z  INFO 641 --- [           main] o.s.boot.tomcat.TomcatWebServer          : Tomcat initialized with port 8081 (http)
2026-10-06T22:53:26.815Z  INFO 641 --- [           main] o.s.boot.tomcat.TomcatWebServer          : Tomcat started on port 8081 (http) with context path '/'
2026-10-06T22:53:26.828Z  INFO 641 --- [           main] tapri.TapriApplication                   : Started TapriApplication in 2.504 seconds (process running for 4.112)

A controller

Here is a small controller that is not part of your tasks:

@RestController
public class StationController {
  record Station(String code, String city) {}

  @GetMapping("/version")
  String version() {
    return "Tapri Cloud 0.0.1";
  }

  @GetMapping("/stations")
  List<Station> stations() {
    return List.of(new Station("SBC", "Bengaluru"), new Station("NDLS", "New Delhi"));
  }
}

@RestController makes the class a bean that component scanning finds, and says that a method's return value is the response body. Return a String and the client gets plain text; return a record or a list and Spring writes JSON:

$ curl -i localhost:8081/version
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 17
Date: Tue, 06 Oct 2026 22:53:27 GMT

Tapri Cloud 0.0.1
$ curl localhost:8081/stations
[{"code":"SBC","city":"Bengaluru"},{"code":"NDLS","city":"New Delhi"}]

A path with no mapped method is a 404, which is what the starter's GET /ping answers today:

{"timestamp":"2026-10-06T22:55:05.897Z","status":404,"error":"Not Found","path":"/ping"}

A method that throws is a 500, and the log names your line:

java.lang.UnsupportedOperationException: TODO: return the menu
	at tapri.MenuController.menu(MenuController.java:55) ~[run-classes-17926638260767678805/:na]

Path variables and query parameters

@GetMapping("/stations/{code}")
String station(@PathVariable String code) {
  return "Station " + code.toUpperCase();
}

@GetMapping("/bill")
int bill(@RequestParam int cups) {
  return cups * 25;
}

A path variable is part of the address; a query parameter comes after the ?. Spring converts the text to the parameter's type:

$ curl localhost:8081/stations/sbc
Station SBC
$ curl "localhost:8081/bill?cups=3"
75

A missing or wrong value never reaches your method. Spring answers 400:

$ curl -i "localhost:8081/bill?cups=two"
HTTP/1.1 400 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Tue, 06 Oct 2026 22:53:27 GMT
Connection: close

{"timestamp":"2026-10-06T22:53:27.343Z","status":400,"error":"Bad Request","path":"/bill"}

and the log says why:

2026-10-06T22:53:27.341Z  WARN 641 --- [nio-8081-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Method parameter 'cups': Failed to convert value of type 'java.lang.String' to required type 'int'; For input string: "two"]

Try it

Start the app in the background, call it, then stop it:

cd chapters/02-first-spring-boot-app
run-app &
curl localhost:8081/ping
curl "localhost:8081/menu/under?price=30"
kill %1

Your turn

Your five tasks are at the top; the comment above each method in the code says its task again. Start with task 5 if run-app will not start. Watch two traps: GET /ping needs its mapping as well as its answer, and under keeps prices strictly less than the limit. Kabir asks the last task in interviews: what does the Spring Boot application annotation switch on?

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    MenuController.java: answer GET /ping with pong

    Make GET /ping answer 200 with the plain text pong (exactly that, not JSON), so a station can see Tapri Cloud is up.

    The ping method is already in MenuController, but no URL is mapped to it yet, so today GET /ping answers 404 Not Found.

  2. Task 2

    MenuController.java: list the menu as JSON at GET /menu

    Make GET /menu answer 200 with every item in MENU as a JSON list, in menu order: four items, Masala chai (40) first and Parle-G last.

    Each item is a MenuItem, which Spring writes as an object with name and price.

  3. Task 3

    MenuController.java: greet a station at GET /hello/{station}

    Make GET /hello/{station} answer 200 with the plain text Welcome to Tapri Cloud, then a comma, a space and the station code exactly as it appears in the path.

    GET /hello/SBC answers Welcome to Tapri Cloud, SBC, and GET /hello/NDLS answers Welcome to Tapri Cloud, NDLS.

  4. Task 4

    MenuController.java: the items under a price at GET /menu/under

    Make GET /menu/under?price=N answer 200 with the items in MENU whose price is less than N, as a JSON list in menu order.

    GET /menu/under?price=30 answers Cutting chai then Parle-G. Less than, not equal: GET /menu/under?price=10 answers an empty list, because Parle-G costs exactly 10.

  5. Task 5 · interview question

    Interview: name what @SpringBootApplication switches on

    Make TapriApplication a Spring Boot application class: it must be a Spring Boot configuration, enable auto-configuration and scan for components (the test looks for all three, directly or through one annotation that contains them).

    Today run-app stops with APPLICATION FAILED TO START because no web server bean exists. Be ready to name the three things the Spring Boot application annotation switches on.

What you will learn

  • The application class: SpringApplication.run and @SpringBootApplication start the container and a web server.
  • Controllers: @RestController and @GetMapping connect a URL to a method; what the method returns is the answer, as text or JSON.
  • Path variables and query parameters: values from the URL, already converted to Java types.

More in Spring foundations

Build "Your first Spring Boot app" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.