Spring MVC provides an annotation-based programming model, where the @Controller
and @RestController
components use annotations to express query mappings, query entry, exception handling, and more. Annotated
controllers have flexible method signatures and are not required to extend base classes or implement specific
interfaces. The following example shows a controller defined using annotations:
Java
@Controller
public class HelloController {
@GetMapping("/hello")
public String handle(Model model) {
model.addAttribute("message", "Hello World!");
return "index";
}
}
Kotlin
import org.springframework.ui.set
@Controller
class HelloController {
@GetMapping("/hello")
fun handle(model: Model): String {
model["message"] = "Hello World!"
return "index"
}
}
In the previous example, the method takes a Model
and returns the name of the view as a
String
, but there are many other options that will be described later in this chapter.
In guides and training materials for spring.io uses the
annotation-based programming model described in this section.