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";
}

}

The following definitions explain the code in detail:

  • @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 /welcome URL 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 further in Chapter 6, Building REST APIs with Spring.
..................Content has been hidden....................

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