码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot接收参数

时间:2018-02-28 21:37:32      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:var   rest   conf   require   ppi   under   map   request   return   

接收参数有三个方法。

1、接收id的方法:

@RestController
public class ControllerTest {


    //在这里读取配置文件
    @Autowired
    private Testconfig testconfig;
    //访问路径:http://localhost:8080/hello/5
    @GetMapping(value = "/hello/{id}")
    public String hello(@PathVariable("id") Integer id){
        return "ID:" + id;
    }
}

2、接收参数:

@RestController
public class ControllerTest {


    //在这里读取配置文件
    @Autowired
    private Testconfig testconfig;
    //访问路径:http://localhost:8080/hello?id=1551
    @GetMapping(value = "/hello")
    public String hello(@RequestParam("id") Integer id){
        return "ID:" + id;
    }
}

也可以这样设置,当不传输参数的时候,默认为5:

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

Springboot接收参数

标签:var   rest   conf   require   ppi   under   map   request   return   

原文地址:https://www.cnblogs.com/suiyisuixing/p/8485647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!