Sub topic 11
Adding a First REST Endpoint
This chapter assumes you have a generated Spring Boot project with the spring-boot-starter-web dependency, and that you can start it with ./mvnw spring-boot:run. You should also know basic Java classes, annotations, and HTTP GET requests.
What You Are Building
You will add one class that answers GET /hello with plain text. Spring Boot's auto-configuration already provides the embedded server and the request-handling machinery (Spring MVC). Your only job is to declare a class that maps a URL to a Java method.
The Dependency That Makes It Possible
The web starter brings in Spring MVC, JSON support, and an embedded Tomcat server. The test starter is scoped to test, so it stays out of the packaged application.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>Neither dependency declares a version. The Spring Boot parent manages them.
Writing the Controller
Create HelloController.java in the same package as the main class, com.example.demo.
package com.example.demo;
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, Spring Boot!";
}
}Two annotations do the work:
@RestControllermarks the class as a web controller. Spring registers it as a bean, and each method's return value is written directly into the HTTP response body.@GetMapping("/hello")routesGET /hellorequests to thehello()method.
The returned String becomes the response body with a text/plain content type.
Why the Package Matters
@SpringBootApplication on the main class enables component scanning. Scanning starts at the main class's package and includes all sub-packages. Your controller lives in com.example.demo, so Spring finds it without any extra registration.
A controller placed in a package outside that tree, such as org.other, is never scanned. The application starts normally, but requests to the endpoint return 404.
How a Request Flows
The DispatcherServlet is the front controller that Spring MVC uses. It looks up which handler method matches the URL and HTTP method, then calls it.
Running and Testing It
Start the application, then call the endpoint from a second terminal.
./mvnw spring-boot:run
curl http://localhost:8080/helloThe curl command prints Hello, Spring Boot!. This assumes the default port 8080. If you set server.port=8081 in application.properties, use that port in the URL instead.
Stop the application with Ctrl+C before restarting. Java does not reload your new class into a running process, so a stale instance will not serve the new endpoint.
Common Errors
| Symptom | Likely cause | Fix |
|---|---|---|
| 404 Not Found | Controller outside the scanned package | Move it under `com.example.demo` |
| 404 Not Found | Application running from before the change | Stop and restart |
| `Connection refused` | App not started, or wrong port | Check the startup log for "Tomcat started on port" |
| Response is HTML or a view-name error | `@Controller` used instead of `@RestController` | Use `@RestController` |
| `Port 8080 was already in use` | Another process holds the port | Stop it, or set `server.port` |
Exercise
You add a class annotated with @RestController in the package org.other.web. The main class is in com.example.demo. The app starts without errors, but GET /hello returns 404. Why does this happen, and how do you fix it?
Show answer
@SpringBootApplication enables component scanning from the main class's package (com.example.demo) and its sub-packages only. org.other.web is outside that tree, so the controller is never registered as a bean and no mapping exists for /hello. Fix it by moving the class into com.example.demo or a sub-package such as com.example.demo.web.
Exercise
Add a second endpoint, GET /greet, that returns "Hi!" and make sure you can call it. Which annotation and method do you add, and do you need to change the main class?
Show answer
Add another method to HelloController (or a new @RestController class in the same package tree), for example @GetMapping("/greet") public String greet() { return "Hi!"; }. The main class does not change, because component scanning already covers the package. Restart the app, then run curl http://localhost:8080/greet.