当前位置: 代码迷 >> 综合 >> Spring 注解面面通 之 @PathVariable
  详细解决方案

Spring 注解面面通 之 @PathVariable

热度:59   发布时间:2024-01-17 01:04:09.0

??@PathVariable用于将请求URI模板变量映射到控制器处理方法的参数中。

??注解解析

??① value

????绑定的参数名称,参数值为String类型。

??② name

????绑定的参数名称,参数值为String类型。namevalue可以同时使用,但两者的值需一致,否则会出现错误。

attribute 'name' and its alias 'value' are present with values of [XXX] and [XXX], but only one is permitted
1

??③ required

????URI中是否必须包含指定的值,默认值为true

????requiredtrue时,如果URI中缺少指定的值,则会抛出异常。

????requiredfalse时,如果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的使用方式。

??若文中存在错误和不足,欢迎指正!

  相关解决方案