??@PathVariable
用于将请求URI
模板变量映射到控制器处理方法的参数中。
??注解解析
??① value
:
????绑定的参数名称,参数值为String
类型。
??② name
:
????绑定的参数名称,参数值为String
类型。name
和value
可以同时使用,但两者的值需一致,否则会出现错误。
attribute 'name' and its alias 'value' are present with values of [XXX] and [XXX], but only one is permitted
1
??③ required
:
????URI
中是否必须包含指定的值,默认值为true
。
????required
为true
时,如果URI
中缺少指定的值,则会抛出异常。
????required
为false
时,如果URI
中缺少指定的值,则会返回null
。
??注解示例
??1) 建Controller
,用来演示@PathVariable
使用方法。
package com.arhorchin.securitit.webannotations;import java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;import com.alibaba.fastjson.JSON;/*** @author Securitit.* @note 演示@SessionAttribute使用方法.*/
@Controller
@RequestMapping("/WebAnnotations")
public class PathVariableController {
/*** logger.*/private Logger logger = LoggerFactory.getLogger(PathVariableController.class);/*** 跳转页面.*/@RequestMapping(value = "/PathVariable.html",method = RequestMethod.GET)public ModelAndView pathVariableHtml(HttpServletRequest req, HttpServletResponse res, ModelMap modelMap)throws Exception {
return new ModelAndView("webannotations/PathVariable", modelMap);}/*** 从请求路径中以Map解析方法参数.*/@ResponseBody@RequestMapping(value = "/{Province}/{City}/PathVariableMap.do",method = RequestMethod.GET)public String pathVariableMap(@PathVariable Map<String, String> pathVariableMap) throws Exception {
logger.info("Current value of PathVariable is " + JSON.toJSONString(pathVariableMap));return "Current value of PathVariable is " + JSON.toJSONString(pathVariableMap);}/*** 从请求路径中以单键值形式解析方法参数.*/@ResponseBody@RequestMapping(value = "/{Province}/{City}/PathVariableSingle.do",method = RequestMethod.GET)public String PathVariableSingle(@PathVariable("Province") String province, @PathVariable("City") String city)throws Exception {
logger.info("Current value of PathVariable [Province] is " + province + " | [City] is " + city);return "Current value of PathVariable [Province] is " + province + " | [City] is " + city;}/*** 从请求路径中以单键值形式解析方法参数.*/@ResponseBody@RequestMapping(value = {
"/{Province}/{City}/PathVariableSingleRequiredTrue.do", "/PathVariableSingleRequiredTrue.do" },method = RequestMethod.GET)public String PathVariableSingleRequiredTrue(@PathVariable(value = "Province",required = true) String province,@PathVariable(value = "City",required = true) String city)throws Exception {
logger.info("Current value of PathVariable(required = true) [Province] is " + province + " | [City] is " + city);return "Current value of PathVariable(required = true) [Province] is " + province + " | [City] is " + city;}/*** 从请求路径中以单键值形式解析方法参数.*/@ResponseBody@RequestMapping(value = {
"/{Province}/{City}/PathVariableSingleRequiredFalse.do", "/PathVariableSingleRequiredFalse.do" },method = RequestMethod.GET)public String PathVariableSingleRequiredFalse(@PathVariable(value = "Province",required = false) String province,@PathVariable(value = "City",required = false) String city)throws Exception {
logger.info("Current value of PathVariable(required = false) [Province] is " + province + " | [City] is " + city);return "Current value of PathVariable(required = false) [Province] is " + province + " | [City] is " + city;}}
??2) 建html
,用来演示PathVariableController
示例。
<!doctype html>
<html lang="en"><head><meta charset="UTF-8"><meta name="Author" content="Securitit"><meta name="Keywords" content="-"><meta name="Description" content="Securitit @PathVariable 测试页面"><title>@PathVariable 测试页面</title></head><body><a href="http://localhost:9199/spring-annotations/WebAnnotations/LiNing/DaLian/PathVariableMap.do" target="_blank">点击链接,@PathVariable进行参数绑定,后端以Map解析方法参数.</a><br /><br /><a href="http://localhost:9199/spring-annotations/WebAnnotations/LiNing/DaLian/PathVariableSingle.do" target="_blank">点击链接,@PathVariable进行参数绑定,以单键值形式解析方法参数.</a><br /><br /><a href="http://localhost:9199/spring-annotations/WebAnnotations/PathVariableSingleRequiredTrue.do" target="_blank">点击链接,@PathVariable进行参数绑定,以单键值形式解析方法参数.required=true,未传值时抛出异常.</a><br /><br /><a href="http://localhost:9199/spring-annotations/WebAnnotations/PathVariableSingleRequiredFalse.do" target="_blank">点击链接,@PathVariable进行参数绑定,以单键值形式解析方法参数.required=false,未传值时返回null.</a></body>
</html>
??3) 启动服务,访问http://localhost:9199/spring-annotations/WebAnnotations/PathVariable.html
页面。
??分别点击页面中的链接,查看@PathVariable
对应的效果。
??① 点击链接,@PathVariable进行参数绑定,后端以Map解析方法参数.
??② 点击链接,@PathVariable进行参数绑定,以单键值形式解析方法参数.
??③ 点击链接,@PathVariable进行参数绑定,以单键值形式解析方法参数.required=true,未传值时抛出异常.
???④ 点击链接,@PathVariable进行参数绑定,以单键值形式解析方法参数.required=false,未传值时返回null.
??总结
??在REST
风格API盛行的当下,利用URI
模板变量来设计接口路径变得越来越流行,而依赖Spring MVC框架的情况下,@PathVariable
是不可跳过的,所以要掌握好@PathVariable
的使用方式。
??若文中存在错误和不足,欢迎指正!