AI Hints
Home›Spring Boot Tutorial›The Main Class, Running the App and Your First Endpoint
Spring Boot · Lesson 9 of 120

Your First Endpoint

Hello, world.

⏱ 2 min read▶ Video at 10:54🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

An endpoint is one URL your app answers. Let's build our first one, step by step.

  • One URL
  • One method
  • Our first endpoint

The whole file

Here is the full file. Package, then two imports, then our class. Your editor adds these imports for you, automatically. So from now on, we'll hide the package line, and the imports, to keep the code short and clear.

HelloController.javajava
package com.example.bookshop;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, world!";
    }
}

Hello, world

At Rest Controller tells Spring, this class handles web requests, and returns data. At Get Mapping slash hello connects the URL slash hello, to this method. The method returns a string. Spring sends that string back as the response. Run it. We send a request to slash hello, and get Hello, world, back.

HelloController.javajava
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, world!";
    }
}
Output
Hello, world!
Also uses _curl.sh
curl localhost:8080/hello

Two endpoints

One controller can have many endpoints. Each method gets its own Get Mapping, with its own path. Run it. Slash hello, and slash time, each run their own method. And a path we never mapped, gives a four oh four error, which means not found.

HelloController.javajava
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, world!";
    }

    @GetMapping("/time")
    public String time() {
        return "The shop opens at 9:00";
    }
}
Output
$ curl localhost:8080/hello
Hello, world!
$ curl localhost:8080/time
The shop opens at 9:00
$ curl localhost:8080/price
{
  "timestamp":"2026-11-25T16:17:12.536Z",
  "status":404,
  "error":"Not Found",
  "path":"/price"
}
Also uses _curl.sh
curl localhost:8080/hello
curl localhost:8080/time
curl localhost:8080/price

Summary

At Rest Controller marks a class that answers requests. At Get Mapping links a path to a method, and the return value becomes the response. Next, HTTP methods.

Key takeaways
  • @RestController
  • @GetMapping("/hello")
  • Return = response
Next up: HTTP Methods — GET, POST, PUT, DELETE