Controller and RestFul

5. Controller and RestFul

5.1 controller

  • The complex controller provides the behavior of accessing the application program, which is usually realized by interface definition or annotation definition.
  • The controller is responsible for parsing the user's request and transforming it into a model.
  • In Spring MVC, a controller class can contain multiple methods
  • In Spring MVC, there are many ways to configure the Controller

5.2 two implementation modes of controller

Controller is an interface, which is located at org springframework. web. servlet. Under MVC package, there is only one method in the interface;

public class HelloController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","Hello SpringMVC!");
        return mv;
  • It is an old method to implement the interface Controller and define the Controller
  • Disadvantages: there is only one method in a Controller. If you want multiple methods, you need to define multiple controllers; The way of definition is troublesome;

Implementation using annotations

  • @The Controller annotation type is used to declare that the instance of Spring class is a Controller (another three annotations were mentioned when talking about IOC);

  • Spring can use the scanning mechanism to find all annotation based controller classes in the application. In order to ensure that spring can find your controller, you need to declare component scanning in the configuration file.

    <!-- Automatically scan the specified package and submit all the following annotation classes to IOC Container management -->
    <context:component-scan base-package="com.wyz.controller"/>
  • Add a HelloController class and implement it with annotations;

public class HelloController {
    public String helloController(Model model){
        model.addAttribute("msg","Hello SpringMVC Annotation!");

        return "hello";

It can be found that both of our requests can point to a view, but the results of the page results are different. From here, we can see that the view is reused, and there is a weak coupling relationship between the controller and the view.

5.3 RequestMapping

@The RequestMapping annotation is used to map URLs to a controller class or a specific handler method. Can be used on classes or methods. Used on a class, indicating that all methods in the class that respond to requests take this address as the parent path.

5.4 RestFul style

Restful is a style of resource positioning and resource operation. It's not a standard or agreement, it's just a style. The software designed based on this style can be more concise, more hierarchical, and easier to implement caching and other mechanisms.


Resources: all things on the Internet can be abstracted as resources

Resource operation: use POST, DELETE, PUT and GET to operate resources with different methods.

Add, delete, modify and query respectively.

Operate resources in traditional way: achieve different effects through different parameters! Single method, post and get Query, GET New, POST Update, POST Delete, GET or POST

Using RESTful operation resources: different effects can be achieved through different request methods! As follows: the request address is the same, but the function can be different! Query, GET New, POST Update, PUT DELETE


public class RestFulController {
    public String test1(@PathVariable int a,@PathVariable int b, Model model){
        int result = a + b;
        model.addAttribute("msg","The result is"+result);
        return "test1";

What are the benefits of using path variables?

  • Make the path more concise;
  • It is more convenient to obtain parameters, and the framework will automatically carry out type conversion.
  • Access parameters can be constrained by the type of path variable. If the type is different, the corresponding request method cannot be accessed. For example, if the access path here is / commit/1/a, the path does not match the method, rather than parameter conversion failure.

Modify the parameter type and test again

public class RestFulController {
    public String test1(@PathVariable int a,@PathVariable String b, Model model){
        String result = a + b;
        model.addAttribute("msg","The result is"+result);
        return "test1";

Use the method property to specify the request type

It is used to restrict the type of request and narrow the request range. Specify the type of request predicate, such as GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE, etc

@RequestMapping(value = "/hello/{a}/{b}",method = {RequestMethod.POST})
public String test2(@PathVariable int a,@PathVariable int b,Model model){
    model.addAttribute("msg","test POST");
    return "test1";

The @ RequestMapping annotation of Spring MVC can handle HTTP requests, such as GET, PUT, POST, DELETE and PATCH.

All address bar requests will be of HTTP GET type by default.

Method level annotation variants are as follows: combined annotation


@GetMapping is a combined annotation, which is often used!

It acts as a shortcut to @ RequestMapping(method =RequestMethod.GET).

Keywords: Spring MVC

Added by poring on Wed, 09 Mar 2022 03:12:52 +0200