SpringMVC中@PathVariable与@PathParam

SpringMVC中@PathVariable与@PathParam

@RequestParam@PathVariable 注解是用于从 request 中接收请求的,两个都可以接收参数,关键点不同的是@RequestParam 是从 request 里面拿取值,而 @PathVariable 是从一个URI模板里面来填充。

@PathVariable

访问下面链接时:

http://localhost:8080//user/22

通过 URI 模板来填充

// 通过 URI 模板来填充
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
@ResponseBody
private Map<String, Object> userId( HttpServletRequest request,@PathVariable("id") int idnum{
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("idnum", idnum);
		return modelMap;
}


@PathParam

访问下链接时

http://localhost:8080/user?id=22
//获取 request 里的值
@RequestMapping(value = "/user", method = RequestMethod.GET)
@ResponseBody
private Map<String, Object> getbyid( HttpServletRequest request,@RequestParam(value="id", required=true)int idnum){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("idnum", idnum);
		return modelMap;
}

//我们只能获取 ?id 的值,如果是 POST 方式,我们也可以获取指定值


你可能感兴趣的:(SpringBoot框架)