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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset