Associating a route to a controller method

In this recipe, you will learn how to define a controller method to be executed for a given route.

How to do it…

Here are the steps for creating a controller method for a given route:

  1. Create a controller class in your controller package (for example, com.springcookbook.controller). This is a normal Java class annotated with @Controller:
    @Controller
    public class UserController {
    ...
    }
  2. Add a controller method. This is a standard Java method annotated with @RequestMapping, which takes the route as a parameter:
    @RequestMapping("/user/list")
    public void userList() {
    ...
    }

How it works…

A request with the /user/list route will execute the userList()method.

Get Spring Cookbook 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.