SpringBoot接收前端参数的三种方法

都是以前的笔记了,有时间就整理出来了,SpringBoot接收前端参数的三种方法,首先第一种代码:

@RestController
public class ControllerTest {
 //访问路径:http://localhost:8080/hello/5
    @GetMapping(value = "/hello/{id}")
    public String hello(@PathVariable("id") Integer id){
        return "ID:" + id;
    }
}

第二种:

  可以通过注解@RequestParam("xxx")来绑定指定的参数,在这里声明的参数为ids,但是@RequestParam()注解中确实id,这个时候前端传的值就应该为id,如果前台传送的是ids的话,在这里是接收不到的。

@RestController
public class ControllerTest {
 //访问路径:http://localhost:8080/hello?id=1551
    @GetMapping(value = "/hello")
    public String hello(@RequestParam("id") Integer ids){
        return "ID:" + id;
    }
}

第三种像这种,如果传进来的参数为空的话,可以设置为默认的:

    //访问路径:http://localhost:8080/hello?id=1551
    @GetMapping(value = "/hello")   
    public String hello(@RequestParam(value = "id", required = false, defaultValue = "5") Integer id){       
           return "ID:" + id;
    }

猜你喜欢

转载自www.cnblogs.com/itiande/p/9505227.html