- Mastering Spring 5.0
- Ranga Rao Karanam
- 141字
- 2021-07-02 22:12:09
Creating a Spring MVC controller
Let's create a simple Spring MVC controller as follows:
@Controller
public class BasicController {
@RequestMapping(value = "/welcome")
@ResponseBody
public String welcome() {
return "Welcome to Spring MVC";
}
}
A few important things to note here are as follows:
- @Controller: This defines a Spring MVC controller that can contain request mappings--mapping URLs to controller methods.
- @RequestMapping(value = "/welcome"): This defines a mapping of the URL /welcome to the welcome method. When the browser sends a request to /welcome, Spring MVC does the magic and executes the welcome method.
- @ResponseBody: In this specific context, the text returned by the welcome method is sent out to the browser as the response content. @ResponseBody does a lot of magic--especially in the context of REST services. We will discuss this in Chapter 5, Building Microservices with Spring Boot.