![](/img/trans.png)
[英]How do I handle an unknown number of RequestParams in Spring Boot REST?
[英]Is there a way to shorten boolean RequestParams in Spring Boot?
感謝所有試圖提供幫助的人:)
背景資料:
我將 spring 框架版本 2.4.3 與 Java (和 Maven)一起使用
對於我的問題:是否可以將http://localhost:8080/api/v1/example?admin=true&superPrivilege=true
為http://localhost:8080/api/v1/example?admin&superPrivilege
的東西。
所以我想要的是使用 boolean 參數作為標志。
如果設置了參數,則如果不是 False,則將其計為 true。 這在 Spring 引導中可能嗎?
我實際上不知道谷歌是為了什么,因為我對任何類型的 Web 開發都是新手。
(而且我在最后幾天確實嘗試了幾個小時 xD)
另一種方法可能是:
@GetMapping("/demo")
public String demo(@RequestParam Map<String, String> parameters) {
if (parameters.containsKey("key")) {
System.out.println("I has key");
}
return "hello";
}
您可以使用Optional<>
檢查 url 中是否存在參數
@GetMapping
public ResponseEntity<?>dataGet(@RequestParam("admin") Optional<Boolean> admin) {
if(admin.isEmpty()) {
// Do anything //without admin role
}
else{
// Do anything with admin role
}
}
此外,如果您傳遞參數,沒有任何值,它將為空,因此您可以進行相應檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.