ZPY博客

SpringBoot中常用注解@ PathVaribale / @ RequestParam controller里接受参数介绍

注解的作用为:

@PathVaribale获取url中的数据

@RequestParam获取请求参数的值

@PathVaribale获取url中的数据

如果我们需要在URL有多个参数需要获取,如本地主机:8080 /hello/98/wojiushimogui则如下代码所示来做就可以了

@RestController
public class HelloController {
    @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)
    public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){
        return "id:"+id+" name:"+name;
    }
}

@RequestParam获取请求参数的值

如果在URL中有多个参数,即类似于本地主机:8080 /hello?id=98 &&name= wojiushimogui这样的链接,同样可以这样来处理具体代码如下:

/**
 * Created by wuranghao on 2017/4/7.
 */
@RestController
public class HelloController {
    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String sayHello(@RequestParam("id") Integer id,@RequestParam("name") String name){
        return "id:"+id+ " name:"+name;
    }
}