emanjusaka —— 彼岸花开可奈何

彼岸花开可奈何

微知识:@PathVariable 注解

2024-03-27

@PathVariable 注解

@PathVariable 是 Spring MVC 中的一个注解,用于处理 HTTP 请求中的 URI 路径变量。

支持 RequestMap 带注释的处理程序方法。如果方法参数是 Map<String, String>,则映射将填充所有路径变量名称和值。

 package top.emanjusaka;
 ​
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RestController;
 ​
 import java.util.Map;
 ​
 /**
  * @Author emanjusaka
  * @Date 2024/3/27 17:49
  * @Version 1.0
  */
 @RestController
 public class MyController {
     @GetMapping("/test/{type}/{id}")
     public void test(@PathVariable Map<String, String> pathVariables) {
         String type = pathVariables.get("type");
         String id = pathVariables.get("id");
         // ...
     }
 ​
 }

在上述示例中,虽然 @PathVariable 没有直接应用于 Map 参数的键上,但在处理 /test/{type}/{id} 这样的请求时,Spring MVC 会将路径中的 type 和 id 分别注入到 Map 中。虽然支持上面这种方式,但并不太建议这么使用。通常还是建议直接将@PathVariable注解应用于具体的参数上。

 package top.emanjusaka;
 ​
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RestController;
 ​
 /**
  * @Author emanjusaka
  * @Date 2024/3/27 17:49
  * @Version 1.0
  */
 @RestController
 public class MyController {
     @GetMapping("/test/{type}/{id}")
     public void test(@PathVariable("type") String type,
                      @PathVariable("id") String id) {
         // ...
 ​
     }
 ​
 }

注解配置:

 /*
  * Copyright 2002-2018 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *      https://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 ​
 package org.springframework.web.bind.annotation;
 ​
 import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 ​
 import org.springframework.core.annotation.AliasFor;
 ​
 ​
 @Target(ElementType.PARAMETER)
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 public @interface PathVariable {
 ​
   /**
    * Alias for {@link #name}.
    */
   @AliasFor("name")
   String value() default "";
 ​
   /**
      * 要绑定的路径变量的名称。 
    * @since 4.3.3
    */
   @AliasFor("value")
   String name() default "";
 ​
   /**
    * 是否需要路径变量。
      * 默认为true,如果传入请求中缺少路径变量,则会引发异常。如果您更喜欢null或Java8 java.util.可选,请将其切换为false。
      * 在这种情况下是可选的。例如,在服务于不同请求的ModelAtcade方法上。
    * @since 4.3.3
    */
   boolean required() default true;
 ​
 }

在技术的星河中遨游,我们互为引路星辰,共同追逐成长的光芒。愿本文的洞见能触动您的思绪,若有所共鸣,请以点赞之手,轻抚赞同的弦。

原文地址: https://www.emanjusaka.top/2024/03/spring-mvc-annotation-pathvariable

微信公众号:emanjusaka的编程栈