SpringBoot用实体接收Get请求传递过来的多个参数(绝对可用)_b_just的博客-CSDN博客_springboot 接收多个参数

当参数为基本类型时

@GetMapping("/example1")
public void example1(Float money, String product){
    System.out.println("product:"+ product);//product:洗洁精
    System.out.println("money:"+ money);//money:123.0
}
//请求url:http://localhost:8888/example1?money=123&product=洗洁精

当参数为数组

 @GetMapping("/example2")
    public void example2(String[] keywords){
        if (keywords != null){
            for (int i=0; i<keywords.length; i++){
                System.out.println(keywords[i]);//123 456
            }
        }
    }
    //请求url:http://localhost:8888/example2?keywords=123,456

当参数为简单对象时

@GetMapping("/example3")
    public void example3(SubTest1 subTest1){
        System.out.println(subTest1);//SubTest1{content='测试内容'}
    }
    //请求url:http://localhost:8888/example3?content=测试内容
public class SubTest1 {
    private String content;
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    @Override
    public String toString() {
        return "SubTest1{" +
                "content='" + content + '\'' +
                '}';
    }
}

当参数的对象中嵌套着对象,对象中的属性为list和map时

@GetMapping("/example4")
    public void example4(TestDto testDto){
        System.out.println(testDto);//TestDto{title='测试标题', subTest=SubTest{ids=[123, 456], map={k=value}}, subTest1=SubTest1{content='测试内容'}}
    }
    //请求url:http://localhost:8888/example4?title=测试标题&subTest.ids[0]=123&subTest.ids[1]=456&subTest.map[k]=value&SubTest1.content=测试内容
public class TestDto {
    private String title;
    private SubTest subTest;
    private SubTest1 subTest1;
    public SubTest1 getSubTest1() {
        return subTest1;
    }
    public void setSubTest1(SubTest1 subTest1) {
        this.subTest1 = subTest1;
    }
    @Override
    public String toString() {
        return "TestDto{" +
                "title='" + title + '\'' +
                ", subTest=" + subTest +
                ", subTest1=" + subTest1 +
                '}';
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public SubTest getSubTest() {
        return subTest;
    }
    public void setSubTest(SubTest subTest) {
        this.subTest = subTest;
    }
}
public class SubTest {
    private List<Long> ids;
    private Map map;
    public Map getMap() {
        return map;
    }
    public void setMap(Map map) {
        this.map = map;
    }
    public List<Long> getIds() {
        return ids;
    }
    public void setIds(List<Long> ids) {
        this.ids = ids;
    }
    @Override
    public String toString() {
        return "SubTest{" +
                "ids=" + ids +
                ", map=" + map +
                '}';
    }
}

//TODO:在直接用list作为参数的时候,程序会报错的;直接用map作为参数的时候,没办法获取到值,都是null,但是不会报错;不知道是姿势错误,还是本身不支持;

map参考:

SpringBoot用实体或Map接收Get请求传递过来的多个参数_缘醉丶莫求的博客-CSDN博客_getmapping接收多个参数@RestController@RequestMapping("/test")public class TestController { @GetMapping("/f1") public void func1(@RequestParam(required = false)Map map){ System.out.println(map); //{name=新一, age=22} } @GetMapping("/f2") pubhttps://blog.csdn.net/qq_37698425/article/details/107667913

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐