First controller
Hub › Java › Beginner › First controller
Goal
Add a single @RestController that answers GET /hello with a plain string. After this page you will know how to wire one HTTP path to one Java method.
Prerequisites
What a controller is
A controller in Spring is a class whose methods handle HTTP requests. You mark the class with @RestController and each method with a mapping annotation (@GetMapping, @PostMapping, and so on). The annotation tells Spring: when a request comes in for this path and method, call this Java method and send its return value back.
@RestController is short for "controller that returns the response body directly." The return value of each method is serialized straight to the response. For text it is sent as plain text; for objects it is sent as JSON (we will see that on the next page).
@GetMapping("/hello") binds the method to GET /hello. Use @PostMapping, @PutMapping, @DeleteMapping for the other verbs.
For the full set of mapping options, see the Spring MVC controller reference.
Code
In the project from the previous page, create a new file:
src/main/java/com/example/itemsapi/HelloController.java
package com.example.itemsapi;
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 from Spring Boot";
}
}Two things to notice:
- The package declaration matches the directory:
com.example.itemsapi. Spring Boot's auto-scan only picks up classes under the package of the main@SpringBootApplicationclass. PuttingHelloControllerin a different package would leave it invisible. - No
mainmethod here. Themainlives inItemsApiApplication.java(Initializr generated it). Spring Boot starts the app from there, then finds your@RestControllerclasses by scanning.
Run the app:
./mvnw spring-boot:runIn a second terminal:
curl http://localhost:8080/helloOutput:
hello from Spring BootStop the server with Ctrl-C.
For frontend developers
@RestController plus @GetMapping("/hello") is the same pattern as an Express handler app.get('/hello', (req, res) => res.send('...')). The annotation is the registration; the method body is the handler.