【WEEK2】 【DAY3】Restful and Controller - The Restful Style【English Version】

2024.3.6 Wednesday

4.2.Restful Style

4.2.1. Concept

Restful is a style for resource location and operation. It is neither a standard nor a protocol, but simply a style. Software designed in this style can be simpler, more structured, and easier to implement caching and other mechanisms. (Optimizing the form of the URL through different request methods than traditional ones)

4.2.2. Functionality

  • Resources: Everything on the Internet can be abstracted as a resource.
  • Resource Operations: Use POST, DELETE, PUT, GET to operate resources. Corresponding to create, delete, update, and retrieve operations respectively.

4.2.3. Traditional way to operate resources

Different effects are achieved through different parameters! Single method, can only use POST and GET

  • http://127.0.0.1/item/queryItem.action?id=1 Retrieve, GET
  • http://127.0.0.1/item/saveItem.action Create, POST
  • http://127.0.0.1/item/updateItem.action Update, POST
  • http://127.0.0.1/item/deleteItem.action?id=1 Delete, GET or POST
    i.e., Different operations can be inferred from the link.

4.2.4. Using RESTful to operate resources

Different effects can be achieved through different request methods.

  • http://127.0.0.1/item/1 Retrieve, GET
  • http://127.0.0.1/item Create, POST
  • http://127.0.0.1/item Update, PUT
  • http://127.0.0.1/item/1 Delete, DELETE
    i.e., The request address is the same, but the functions can differ.

4.2.5. Create RestFulController.java

Insert image description here

1. Code

package com.kuang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RestFulController {
    
//  Traditional way: http://localhost:8080/springmvc_04_controller_war_exploded/add?a=1&b=2
    @RequestMapping("/add")
    public String test1(int a, int b, Model model){
        int sum = a + b;
//Spring MVC will automatically instantiate a Model object to pass values to the view
        model.addAttribute("msg","The result is " + sum);
        return "test";
    }
    
//  Using RestFul: http://localhost:8080/springmvc_04_controller_war_exploded/add_PV/1/2
    @RequestMapping("/add_PV/{a}/{b}")
    public String test2(@PathVariable int a, @PathVariable int b, Model model){
        int sum = a + b;
        model.addAttribute("msg","The result is " + sum);
        return "test";
    }
    
}

2. Result

http://localhost:8080/springmvc_04_controller_war_exploded/add?a=1&b=2
Insert image description here
http://localhost:8080/springmvc_04_controller_war_exploded/add_PV/1/2
Insert image description here
http://localhost:8080/springmvc_04_controller_war_exploded/add_PV/1/“22qwe” The reason for the error display is that the data parameter passed in is different from the variable type in the method.
Insert image description here
No error after modifying to String

public class RestFulController {
//  Using RestFul: http://localhost:8080/springmvc_04_controller_war_exploded/add_PV/1/2
    @RequestMapping("/add_PV/{a}/{b}")
    public String test2(@PathVariable String a, @PathVariable String b, Model model){
        String sum = a + b;
        model.addAttribute("msg","The result is " + sum);
        return "test";
    }

http://localhost:8080/springmvc_04_controller_war_exploded/add_PV/vfdxz/25678vc
Insert image description here

3. Benefits of Using Path Variables

  • Makes the path more concise;
  • It’s more convenient to obtain parameters as the framework will automatically convert types.
  • The type of path variable can constrain the access parameters; if the types do not match, the corresponding request method cannot be accessed, as in this case where the path /commit/1/a does not match the method, rather than failing to convert parameters.

4.2.6. RequestMethod

  • Using the method attribute to specify the request type.
  • RequestMethod is used to constrain the type of request, it can narrow down the scope of requests. Specifies the type of request verb such as GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE, etc.

1. Code

package com.kuang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RestFulController {
//  Using RestFul: http://localhost:8080/springmvc_04_controller_war_exploded/add_PV/1/2
    @RequestMapping(name = "/add_PV/{a}/{b}",method = RequestMethod.DELETE)
//  When there is only one parameter, the name can be omitted; when there are two parameters, both need to be written. Here, the request method is specified as DELETE (default is GET method).
    public String test2(@PathVariable int a, @PathVariable int b, Model model){
        int sum = a + b;
        model.addAttribute("msg","The result is " + sum);
        return "test";
    }
}

2. Result

Since this method should be executed in a GET request method, hence the 405 error.
Insert image description here
It works after changing RequestMethod to GET.

package com.kuang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class RestFulController {

//  Using RestFul: http://localhost:8080/springmvc_04_controller_war_exploded/add_PV/1/2

    @RequestMapping(value = "/add_PV/{a}/{b}",method = {RequestMethod.GET})
//    The method above and the one below are equivalent, either one can be used.
//    @GetMapping("/add_PV/{a}/{b}")
//    @DeleteMapping("/add_PV/{a}/{b}")
//  The name can be omitted when there is only one parameter; both need to be written when there are two. Here, the request method is specified as DELETE (default is GET method).
    public String test2(@PathVariable int a, @PathVariable int b, Model model){
        int sum = a + b;
        model.addAttribute("msg","The result is " + sum);
        return "test";
    }

}

Insert image description here

4.2.7. Summary

  1. The @RequestMapping annotation in Spring MVC can handle HTTP request methods such as GET, PUT, POST, DELETE, and PATCH.
  2. All requests from the address bar are HTTP GET by default.
  3. There are several method-level annotation variants, known as composite annotations:
  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping
  • @PatchMapping
  1. @GetMapping is a composite annotation, which is commonly used.
  2. It acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

4.2.8. Rubber Duck Debugging

Details can be found and understood from the following link
https://baike.baidu.com/item/%E5%B0%8F%E9%BB%84%E9%B8%AD%E8%B0%83%E8%AF%95%E6%B3%95/16569594?fr=ge_ala

  • Scenario one: We’ve all had the experience of asking someone (even someone who may not know programming at all) questions about programming issues and explaining them, but often it’s during the explanation that we come up with the solution ourselves, leaving the other person baffled.
  • Scenario two: A colleague comes to ask you a question, but as they finish explaining the problem or even halfway through, they figure out the answer and walk away, leaving you puzzled.

The above two scenarios are what is known as Rubber Duck Debugging, which is one of the most commonly used debugging methods in our software engineering.
Insert image description here

This concept is said to originate from a story in the book “The Pragmatic Programmer,” where it’s told that a programming master carries a little yellow duck with them. While debugging code, they would place this duck on the table and explain each line of code in detail to it, quickly locating and fixing the problem.

  • 24
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值