SpingMVC learning - Controller&@RequestMapping&RestFul

SpingMVC learning-

Controller

The controller implementation mode is interface or annotation, which is generally implemented by annotation. It is responsible for parsing the user's request, transforming it into a model and returning it to the view parser

The following four annotations are equivalent

  • @Component component
    @Service layer
    @Controller control layer controller
    @Repository dao

The last two blogs used interfaces respectively Spring MVC learning - understanding of the implementation principle of hellosppring MVC And annotation implementation Spring MVC learning - using annotation development There is no need to copy all the code here. You can see the above two blogs for specific steps. Generally, it is implemented in the way of @ Controller annotation during development. Here's the difference between the two:

  • Only one method can be written in a Controller implemented using the Controller interface, and each Controller class needs to be written in springmvc servlet Configure the corresponding bean in the XML configuration file
  • Using the @ Controller annotation, you only need to configure @ Controller on the Controller class, which will be taken over by spring. For all the methods in the annotated class, you can configure @ RequestMapping("xxx") on the class and method. There can be multiple methods in a Controller class

@RequestMapping

@The RequestMapping annotation is used to map the url to the controller or a specific handler method. It can be used on a class or method. When used on a class, it means that all the methods in the class that respond to the request take this address as the parent path.
Use example

@Controller
@RequestMapping("/t3")
public class ControllerTest3 {
    @RequestMapping("/test1")
    public String test1(Model model){
        model.addAttribute("msg","t3/test1");
        return "test";//test.jsp page
    }
    @RequestMapping("/test2")
    public String test2(Model model){
        model.addAttribute("msg","t3/test2");
        return "test";
    }
}

RestFul

Restful is a design style and development method of network application. It is based on HTTP and can be defined in XML format or JSON format. Restful is applicable to the scenario where mobile Internet manufacturers act as business interfaces to realize the function of third-party OTT calling mobile network resources. The action types are adding, changing and deleting the called resources.
Operate resources in traditional ways, such as post and get
Using RestFul to operate resources: different effects can be achieved through different request methods
Features: simple, efficient and safe
1. Each URI represents one resource;
2. The client uses GET, POST, PUT and DELETE verbs to operate the server resources: GET is used to obtain resources, POST is used to create new resources (or update resources), PUT is used to update resources, and DELETE is used to DELETE resources;
3. Operating resources through the expression of operating resources;
4. The representation of resources is XML or HTML;
5. The interaction between client and server is stateless between requests. Every request from client to server must contain the information necessary to understand the request.
Traditional way:

 // customary: http://localhost:8080/add?a=1&b=2
    @RequestMapping("add")
    public String test1(int a, int b, Model model){
        int res = a+b;
        model.addAttribute("msg","The result is"+res);
        return "test";
    }

RestFul mode:

//RestFul:http://localhost:8080/add/a/b
    @RequestMapping("add2/{a}/{b}")
    public String test2(@PathVariable int a,@PathVariable int b, Model model){
        int res = a+b;
        model.addAttribute("msg","The result is"+res);
        return "test";
    }

You can set the parameter method = requestmethod XXX limited resource access method

  // method = RequestMethod.GET qualified resource access method
    @RequestMapping(value = "add2/{a}/{b}",method = RequestMethod.GET)

You can also use annotations for qualification. The above statement is equivalent to

@GetMapping("add2/{a}/{b}")//It can only be accessed through get

Similar are:
@GetMapping
@PostMapping
@DeleteMapping
@PutMapping

Keywords: Java RESTful

Added by m7_b5 on Tue, 01 Mar 2022 11:57:26 +0200