I'm sending to a Spring Controller an Ajax GET request with an Array Parameter,
$.ajax({
url: "getChoices",
dataType: "json",
type: "get",
data: {
'myarg': myarray // JS array of strings, ["a","b","c"]
// Verified to be correct
},
Controller Method that should receive this argument -- the arg name matches:
@ResponseBody
@GetMapping("/getChoices")
public List<KeyValueBean> getChoices(List<String> myarg) {
//...
}
First of all, the way this is written above, I'm getting the error:
[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)
Then I tried making some tweaks:
1) Made the method signature a String[]
, that's not what I want -- I need a List -- but tried it just in case:
public List<KeyValueBean> getChoices(String[] myarg) {
This came into the method, but myarg is NULL and didn't get set correctly.
2) Kept a List<String>
but tried JSON.stringify around the array:
JSON.stringify(myarray)
Same exception: [java.lang.IllegalStateException: No primary or default constructor found for interface java.util.List]
How do I pass an Array arg in an Ajax GET request in Spring Boot?