Brian的雜記
  • Introduction
  • Brian's 雜記
    • My Awesome API
    • FB 大頭貼
    • 硬體雜記
    • PHP
    • project
      • 模擬器
      • WAMP
    • WinMerge
    • 雜記
      • LINQ
      • 方法
      • Grid View
      • namespace
      • global
      • 物件導向
      • Excel
      • VS2017
      • single sign on
      • Master
      • Https
      • 憑證
      • 略過憑證不符
      • NLog
      • 團隊開發
      • .NET Core
      • 共用網路上的芳鄰
      • 爬蟲
      • NPOI
      • RSS
      • 多執行緒
      • 記憶體回收
      • 密碼學
        • BCrypt
        • AES
      • 主機環境建置
      • Session
      • Error
      • IIS 相關
      • 無障礙相關
      • 介面
        • 影像地圖
      • telnet
        • smtp
      • nslookup
      • 協助客戶解決問題
      • 驗證欄位
      • 網站管理
      • 工具整理
    • 正規表示法
    • 影像處理
    • IntelliJ Idea
    • 觀念
      • Clean Code
        • 命名
        • 函式
        • 註解
        • 編排
        • 物件及資料結構
        • 錯誤處理
        • 邊界
        • 單元測試
        • 類別
      • Java 程式風格
      • Design Pattern
        • 單例模式
      • 同步
      • 畫圖
        • ER-Model
        • 類別圖
        • Use Case
        • 有限狀態機
      • 資料 API 文件 分析
      • CORS & SSL
      • 利用DISC幫助溝通
      • OAS
    • 檔案上傳
      • FileStore
      • App Engine
      • Google Storage
    • OAuth vs Open ID
    • MIME
    • 虛擬桌面
    • 待看資料
    • Selenium
    • CDN
    • HTTP
    • 編碼
    • 2nd-ML100Days
      • jupyter
    • 微服務
      • 設計
        • 1 ~ 5
        • 6
        • 7
        • 8
        • 9
    • Gradle
    • Maven
    • Error
    • 批次檔 BAT
    • Kurento
    • WebSocket & WebRTC
  • 需求面能力
    • User Story
  • Google Cloud Platform
    • Compute Engine
  • Python
    • 基本語法
    • Pandas
    • 套件
    • Matplotlib
    • Encoder
    • jupyter
  • Java
    • Java
      • File
      • Exception
      • 物件導向觀念
      • 加密
      • HTTP
      • 集合
      • Stream()
      • Web
      • ResultSet
      • JDK6
      • JDK8
    • 讀取、複寫MP3 Tag
    • Log4j2
    • Servlet
      • 容器
    • JSP
    • JBOSS
    • JWT
    • PreparedStatement
    • Error
    • Spring
      • Spring Boot
        • @Value
        • Build
      • RequestParameter
      • Error
      • Autowired
      • JPA
      • FeignClient
      • WebSocket
      • thymeleaf
      • Security
      • Test
      • Scheduled
      • Redirect
    • IntelliJ
  • Linux
    • Linux
    • Shell Script
    • Cygwin(在Windows執行Linux指令)
  • 前端
    • HTML
      • Link
    • CSS
      • Position
      • padding color
      • display
    • JS
      • jQuery
        • Select2
      • fancybox
      • ES6
      • 效能
      • GoogleMap API
        • Marker
        • InfoWindow
      • 事件
      • CKEditor
      • TGOS
      • JSON
      • QRcode
      • 核心概念
        • 物件 變數 型別
          • number
          • String
          • boolean
          • null & undefined
          • Symbol
        • JS 物件概念
        • 深入理解JS 函式物件
        • 更多ES2015/ES6 全新語言特性
      • Promise
    • 效果應用
  • 資料庫
    • 注意事項
    • MariaDB
    • MySQL
      • inner join 和 join
      • 字串比對
      • 倒數資料
    • SQL
      • DDL 資料定義語言
      • DML 資料操縱語言
      • DCL 資料控制語言
      • TCL 交易控制語言
      • T-SQL
      • CTE
      • JOIN
    • Oracle
    • MSSQL 操作
      • 新增使用者
      • SQL 指令
      • Sequence
    • 差異比較
    • MyBatis
    • Workbench 操作
    • SQL Injection
  • 版本控制
    • Gitlab
      • sign up
      • sign in
      • add project
      • add members
    • SourceGit
      • install
    • SmartGit
      • install
      • operate
      • git 操作雜記
    • TortoiseGit
    • Git
    • TFS
    • SVN
  • Test
    • 軟體測試原則
    • 演算法
    • XMind
      • install
    • Jenkins
      • 建置
    • HTTPie
    • Postman
    • 測試驅動開發
    • 撰寫測試的觀念
    • 測試框架
    • IoC & DI
    • 隔絕相依性的方式
    • JUnit
    • NUnit
    • 習慣
    • 虛設常式
  • Angular
    • hello world
    • ng-options
    • ES6
    • Build & Conponect
    • HttpClient
    • 部署
  • ASP.NET Web Form
    • Chapter 2
      • 2-1
        • 小東西
    • 略過請求驗證
  • Go
Powered by GitBook
On this page
  • RequestMapping
  • 簡單映射 A Simple Mapping (如何接到 QueryString 的參數)
  • 指定請求參數 Specifying the Request Parameter Name
  • 製作可選的請求參數 Making an Optional Request Parameter
  • 請求參數的默認值 A Default Value for the Request Parameter
  • 映射所有參數 Mapping All Parameters
  • 映射多值參數 Mapping a Multi-Value Parameter
  • @RequestParam vs @PathVariable
  • 查詢參數 & URI路徑
  • 編碼與精確值
  • 可選值

Was this helpful?

  1. Java
  2. Spring

RequestParameter

PreviousBuildNextError

Last updated 5 years ago

Was this helpful?

參考資料:() ModifyDate:2019/03/02

@RequestParam 和 屬性,還有 @RequestParam & @PathVariable 的區別

RequestMapping

參考資料:()

@RequestMapping(value = "/ex/foos", method = RequestMethod.GET)
@ResponseBody
public String getFoosBySimplePath(){
    return "Get some Foos";
}

簡單映射 A Simple Mapping (如何接到 QueryString 的參數)

QueryString 的 key 和程式內的 變數名稱 相同,則可以直接Mapping

@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam String id) {
    return "ID: " + id;
}
http://localhost:8080/api/foos?id=abc
----
ID: abc

指定請求參數 Specifying the Request Parameter Name

如果希望 QueryString 的 key 和程式內的 變數名稱 不同,則可以使用 name 這個屬性進行配置。

@PostMapping("/api/foos")
@ResponseBody
public String addFoo(@RequestParam(name = "id") String fooId, @RequestParam String name) { 
    return "ID: " + fooId + " Name: " + name;
}

這三個都可以達到同樣效果

@RequestParam(name = "id") String fooId
@RequestParam(value = "id") String fooId
@RequestParam("id") String fooId

製作可選的請求參數 Making an Optional Request Parameter

通常 @RequestParam 會默認為必填,這樣倘若沒有打參數,就會跳出錯誤

GET /api/foos HTTP/1.1
-----
400 Bad Request
Required String parameter 'id' is not present

所以我們可以將配置,改為可選

@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam(required = false) String id) { 
    return "ID: " + id;
}

這樣,不論是

http://localhost:8080/api/foos?id=abc
----
ID: abc



http://localhost:8080/api/foos
----
ID: null

該呼叫都可以正常調用,但是沒有傳的參數會綁定為Null。

請求參數的默認值 A Default Value for the Request Parameter

利用屬性 defaultValue 設置 默認值

@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam(defaultValue = "test") String id) {
    return "ID: " + id;
}
http://localhost:8080/api/foos
----
ID: test


http://localhost:8080/api/foos?id=abc
----
ID: abc

當設置 defaultValue 這個屬性時,就像 required = false,而事實上 也的確被設置為 false。

映射所有參數 Mapping All Parameters

直接將 QueryString 的所有參數 key,value ,都放到一個 Map 裡

@PostMapping("/api/foos")
@ResponseBody
public String updateFoos(@RequestParam Map<String,String> allParams) {
    return "Parameters are " + allParams.entrySet();
}
curl -X POST -F 'name=abc' -F 'id=123' http://localhost:8080/api/foos
-----
Parameters are {[name=abc], [id=123]}
http://localhost:8080/api/foos?id=12&magic=Brian
-----
Parameters are [id=12, magic=Brian]

映射多值參數 Mapping a Multi-Value Parameter

QueryString 的 一個 key 可以有多個 value

@GetMapping("/api/foos")
@ResponseBody
public String getFoos(@RequestParam List<String> id) {
    return "IDs are " + id + " size = " + id.size();
}

分別可以使用下列兩種

http://localhost:8080/api/foos?id=1,2,3
----
IDs are [1, 2, 3] size = 3

http://localhost:8080/api/foos?id=1&id=4
----
IDs are [1, 4] size = 2

但是不可以同時使用

http://localhost:8080/api/foos?id=1,2,3&id=4
-----
IDs are [1,2,3, 4] size = 2

@RequestParam vs @PathVariable

@RequestParam 和 @PathVariable 都可以從URI提取值

查詢參數 & URI路徑

@RequestParam 是從查詢參數(QueryString) @PathVariable 是從URI路徑(ResourcePath)

@PathVariable

@GetMapping("/foos/{id}")
@ResponseBody
public String getFooById(@PathVariable String id) {
    return "ID: " + id;
}
http://localhost:8080/foos/abc
----
ID: abc

@RequestParam

@GetMapping("/foos")
@ResponseBody
public String getFooByIdUsingQueryParam(@RequestParam String id) {
    return "ID: " + id;
}
http://localhost:8080/foos?id=abc
----
ID: abc

編碼與精確值

@PathVariable 是從URI路徑 獲得參數,所以不會被編碼。 @RequestParam 則會被編碼。

@PathVariable

http://localhost:8080/foos/ab+c
----
ID: ab+c

@RequestParam

http://localhost:8080/foos?id=ab+c
----
ID: ab c

可選值

@PathVariable 從 Spring 4.3.3 開始,required 這個屬性讓其變得可選。

用大括號去配置多個路徑

@GetMapping({"/myfoos/optional", "/myfoos/optional/{id}"}) //注意大括號
@ResponseBody
public String getFooByOptionalId(@PathVariable(required = false) String id){
    return "ID: " + id;
}
http://localhost:8080/myfoos/optional/abc
----
ID: abc

http://localhost:8080/myfoos/optional
----
ID: null

當使用 @PathVariable 的可選屬性時,要注意路徑衝突

https://www.baeldung.com/spring-request-param
https://www.baeldung.com/spring-requestmapping