SpringBoot basics - parameter passing 2

Create project

Create a Spring Boot project

①Create entity.User entity class under com.exampe

@Data
//已导入Lombok以来的情况下可以写@Data注解
//否则User实体类加上set,get,toString方法
public class User {
    private Long id;
    private String name;
}

②Create a test.Test class under com.exampe

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

normal object

①Non-json request

Backend content:

    //这个方法在新建的Test类里面
    @RequestMapping("demo4")
    public User demo4(User user){
        return user;
    }

Front-end access:  http://localhost:8080/test/demo4?id=12&name=AGi 

Visit result:

 ②json request method

 Backend content (need to add @RequestBody annotation):

    //这个方法在新建的Test类里面
    @RequestMapping("demo4")
    public User demo4(@RequestBody User user){
        return user;
    }

Front-end access ( using postman ):

array

①Non-json form

Backend content:

    @RequestMapping("demo5")
    public Long[] demo5(Long[] ids){
        return ids;
    }

Front-end access:  http://localhost:8080/test/demo5?ids=1,2,3,4  or  http://localhost:8080/test/demo5?ids=1&ids=2&ids=3&ids=4

Visit result:

 ②json format

Backend content:

    @RequestMapping("demo6")
    public Long[] demo6(@RequestBody Long[] ids){
        return ids;
    }

Front-end access ( using postman ):

Collection List 

 ①Non-json form

Backend content ( @RequestParam annotation is indispensable ):

    @RequestMapping("demo7")
    public List<Long> demo7(@RequestParam List<Long>  ids){
        return ids;
    }

Front-end access: http://localhost:8080/test/demo7?ids=1,2,3,4  or  http://localhost:8080/test/demo7?ids=1&ids=2&ids=3&ids=4

Visit result:

 ②json format

Backend content:

    @RequestMapping("demo7")
    public List<Long> demo7(@RequestBody List<Long>  ids){
        return ids;
    }

Front-end access ( using postman ):

Lists are used in much the same way as arrays. 

Guess you like

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