SpringBoot basics - parameter passing

Create project

Create a Spring Boot project

Create the controller.Test class under the com.example.demo package

@RestController
@RequestMapping("test")
public class Test {
    
}

Traditional way of passing parameters

①Create a demo method in the Test class and add the @RequestMapping("demo") annotation

@RestController
@RequestMapping("test")
public class Test {
    @RequestMapping("demo")
    public String demo(String name){
        return name;
    }
}

②Start the project and visit  http://localhost:8080/test/demo?name=1234  

③ When the name is changed to name1 when accessing

 It was found that the browser did not return a value. It can be seen that in the traditional way, the data can be received only when the parameter name input by the browser is the same as the back-end parameter name, otherwise it cannot.

④ It can be accessed without passing parameters, but it will not return data

In traditional transmission methods, parameters are optional.

@RequestParam annotation

①Create a demo2 method in the Test class, add the @RequestMapping("demo2") annotation , and add the @RequestParam annotation in front of the parameter . In this case, name2 is required

@RestController
@RequestMapping("test")
public class Test {
    @RequestMapping("demo2")
    public String demo2(@RequestParam String name2){
        return name2+"\t";
    }
}

 If name2 is not passed, an error will be reported

②Edit the demo2 method and add the @RequestParam("name33") annotation in front of the parameter. In this case, when accessing the front-end, you cannot use name3 as a parameter, you must use the name33 in the annotation.

@RequestMapping("demo2")
    public String demo2(@RequestParam("name33") String name3){
        return name3+"\t";
    }

Using name3 or not passing parameters will report an error 

③ Edit the demo2 method and add the @RequestParam(value = "name44") annotation in front of the parameter. In this case, when accessing the front-end, you cannot use name4 as a parameter, but use the name44 in the annotation. This is equivalent to @RequestParam("name44")

@RequestMapping("demo2")
    public String demo2(@RequestParam(value = "name44") String name4){
        return name4+"\t";
    }

Similarly, using name4 or no parameters will report an error 

 ④ Edit the demo2 method, and add the @RequestParam(name = "name55") annotation in front of the parameter. In this case, when accessing the front-end, name5 cannot be used as a parameter, and the name55 in the annotation must be used. This is equivalent to @RequestParam("name55")

@RequestMapping("demo2")
    public String demo2(@RequestParam(name = "name55") String name5){
        return name5+"\t";
    }

 Similarly, using name5 or no parameters will report an error 

 [ Summary ] It can be seen that @RequestParam("") , @RequestParam(name = "") , @RequestParam(value = "") have the same effect.

⑤ Edit the demo2 method, and add @RequestParam(name = "name66", required = false ) in front of the parameter . In this case, name6 is optional. Using name6 as a parameter and not passing a parameter when accessing the front-end is equivalent to no parameter being passed.

@RequestMapping("demo2")
    public String demo2(@RequestParam(value = "name66",required = false) String name6){
        return name6+"\t";
    }

 

 ⑦Edit the demo2 method, add @RequestParam(name = "name77", defaultValue = "Hahaha" ) in front of the parameter . In this case, using name7 as a parameter or not passing a parameter when accessing the front end is equivalent to no parameter passing , and the backend returns the value of defaultValue by default.

@RequestMapping("demo2")
    public String demo2(@RequestParam(value = "name77",defaultValue = "哈哈哈") String name7){
        return name7+"\t";
    }

There is always a return value in this case.

@PathVariable

Create method 3, add annotations

@RequestMapping(value ={
            "demo3/{name1}/{name2}/{name3}",
            //因为name3是可选参数,所以要考虑没有传输name3的情况
            "demo3/{name1}/{name2}"})
    public String demo3(
            // 可选 参数
            String name,
            // 必选 参数
            @PathVariable String name1,
            // 必选 参数
            @PathVariable("name2") String name2,
            //加required = false之后变成 可选 参数
            @PathVariable(value = "name3",required = false) String name3
    ){
        return name+"\t"
                +name1+"\t"
                +name2+"\t"
                +name3+"\t";
    }

Popular when the name3 parameter is passed, orange when not passed 

 ①When passing name3, http://localhost:8080/test/demo3/ALEX/LINUX/C?name=Admin 

 ②If name3 is not passed,  http://localhost:8080/test/demo3/ALEX/LINUX?name=Admin

Guess you like

Origin blog.csdn.net/weixin_46899412/article/details/123500582