@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的使用方式。
若文中存在错误和不足,欢迎指正!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/85469.html