SpringBoot接收GET請求中的數組、Map集合參數

本文主要有以下內容:

  1. SpringBoot 如何接收HTTP Get請求中的數組參數?

  2. [java.lang.IllegalStateException: No primary or default constructor found for interface java.util.List] 報錯如何解決?

一、SpringBoot2.2.4接收數組參數

需要配合使用 @RequestParam註解

注: 如果你使用 Domain對象,則沒有這個限制。

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

驗證結果: 
http://localhost:8080/api/foos?id=1,2,3
----
IDs are [1,2,3]

參考連接

二、SpringBoot接收數組參數報錯

[java.lang.IllegalStateException: No primary or default constructor found for interface 
 java.util.List]

Caused by: java.lang.NoSuchMethodException: java.util.List.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:216)

解決方案:

原因就是沒有使用 @RequestParam註解

報錯參考鏈接:
SpingBoot參數接收List數組報錯

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章