2024.3.6 Wednesday
Contents
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
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
http://localhost:8080/springmvc_04_controller_war_exploded/add_PV/1/2
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.
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
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.
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";
}
}
4.2.7. Summary
- The
@RequestMapping
annotation in Spring MVC can handle HTTP request methods such as GET, PUT, POST, DELETE, and PATCH. - All requests from the address bar are
HTTP GET
by default. - There are several method-level annotation variants, known as composite annotations:
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
- @PatchMapping
- @GetMapping is a composite annotation, which is commonly used.
- 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.
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.