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 ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.