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;
}
上一篇:ACM学习历程—Hihocoder [Offer收割]编程练习赛1


下一篇:System.currentTimeMillis()计算方式与时间的单位转换