There are three ways SpringBoot receives parameters, and SpringBoot accesses static resources.

1. Path parameters:

Receive: http:local:8080/page/sub/1

1. Write the required parameters at the route

2. The parameter list is annotated with @PathVariable . If the parameter name written on the path is inconsistent with the name received by the function, you can use the value parameter to be consistent with the name on the path.

    //查询子分类的分页数据
    @GetMapping("/page/sub/{id}") //id指得就是那个rootId
    public void test(@PathVariable(value = "id") Integer idValue) {
        System.out.println(idValue);
    }

two,? The splicing parameters:

接收: http:local:8080/page/sub?page=1&count=10

1. @RequestParam receives the corresponding parameter, and if the name is inconsistent, you can use value to keep it consistent

2. Just write a few parameters as many as you need

3. Use defaultValue to specify the default value. The value must be wrapped with "" , even though it is of type Integer.

    @GetMapping("/page/sub") //id指得就是那个rootId
    @LoginRequired
    public void test(@RequestParam(defaultValue = "0") Integer page,
                     @RequestParam(defaultValue = "10") Integer count) {
        System.out.println(page+" "+count);
    }

3. Receiving object:

Receive: http:local:8080/insert

1. Define a class that is consistent with the name and data type of the object sent from the front end.

2. The @RequestBody annotation indicates that an object is to be received

3. If the custom class has validation annotations, it is necessary to add the @Validated annotation before the object class

Custom class:

@Data
public class testForm{
    @NotNull
    private Integer id;
}

Receive class:

    @PostMapping("/insert")
    public void test(@RequestBody @Validated testForm form) {
          System.out.println(form.getId());
    }

4. Access static resources:

1. Add static resource management dependencies:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2. Create a directory (my name is img) under static under the resource directory, and put the picture in it:

3. Visit http://localhost:8080/imgs/4.png to access the corresponding picture.

Guess you like

Origin blog.csdn.net/weixin_60414376/article/details/126800121