<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Spring Boot `@PathVariable`教程 > 原文: [http://zetcode.com/springboot/pathvariable/](http://zetcode.com/springboot/pathvariable/) Spring Boot `@PathVariable`教程展示了如何讀取帶有`@PathVariable`注解的 URL 模板變量。 我們創建一個 Spring Boot RESTful 應用來演示注解。 Spring 是流行的 Java 應用框架,而 Spring Boot 是 Spring 的演進,可以幫助輕松地創建獨立的,生產級的基于 Spring 的應用。 ## `@PathVariable` `@PathVariable`是一個 Spring 注解,它指示方法參數應綁定到 URI 模板變量。 它具有以下可選元素: * `name` - 要綁定到的路徑變量的名稱 * `required` - 指示路徑變量是否為必需 * `value` - 名稱的別名 ## Spring Boot `@PathVariable`示例 以下示例創建一個使用`@PathVariable`的 Spring Boot Web 應用。 該應用接收一個 URL,從該 URL 構建到客戶端的文本響應。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───controller │ │ MyController.java │ └───resources └───test └───java ``` 這是 Spring Boot 應用的項目結構。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>pathvariableex</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project> ``` 這是 Maven 構建文件。 `spring-boot-starter-web`是使用 Spring MVC 構建 Web 應用的入門工具。 它使用 Tomcat 作為默認的嵌入式容器。 `spring-boot-devtools`是在開發 Spring Boot 應用時有用的構件。 它允許自動重啟或實時重新加載應用。 該應用打包到一個 JAR 文件中。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @RequestMapping(path="/{name}/{age}") public String getMessage(@PathVariable("name") String name, @PathVariable("age") String age) { var msg = String.format("%s is %s years old", name, age); return msg; } } ``` 控制器處理來自客戶端的請求。 它從請求的 URL 中讀取兩個值。 ```java @RestController public class MyController { ``` 我們有一個 RESTful Web 應用。 ```java @RequestMapping(path="/{name}/{age}") public String getMessage(@PathVariable("name") String name, @PathVariable("age") String age) { ``` 使用`@PathVariable`注解,我們將請求 URL 模板路徑變量綁定到方法變量。 例如,對于`/July/28/` URL,七月值綁定到`name`變量,而 28 值綁定到`age`變量。 ```java var msg = String.format("%s is %s years old", name, age); return msg; ``` 我們構建消息并返回。 `com/zetcode/Application.java` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` `Application`是設置 Spring Boot 應用的入口。 `@SpringBootApplication`注解啟用自動配置和組件掃描。 ```java $ mvn spring-boot:run ``` 我們啟動 Spring Boot 應用。 ```java $ curl localhost:8080/Robert/39/ Robert is 39 years old ``` 我們使用`curl`工具向應用創建請求。 該應用響應一條消息。 使用`@PathVariable`從 URL 中提取值。 在本教程中,我們使用 Spring Boot 框架創建了一個 RESTful Web 應用。 我們已經演示了`@PathVariable`的用法。 您可能也對相關教程感興趣: [Spring Boot `@RequestParam`教程](/springboot/requestparam/), [Spring Boot `@ResponseBody`教程](/springboot/responsebody/), [Spring Boot `@RestController`教程](/springboot/restcontrollre/), [Java 教程](/lang/java/),或列出[所有 Spring Boot 教程](/all/#springboot)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看