I was working with JSONs to send a list of objects to a controller in Spring. My controller method is this:
@PostMapping("/fun")
public String fun(List<User> users){
for(User user : users){
System.out.println(user);
}
return "success";
}
And this is my User class:
public class User{
String name;
String password;
int empID;
// getter and setters
}
This is the JSON which I am sending:
[
{
"name":"some-name",
"password":"password",
"empID":2
}
]
My question is, how does JSON knows that this is List of User, because Java type erasure makes it impossible to know which kind of object does a List contains. So even without knowing the object how is JSON is able to map itself to User class.
Edited
What I actually want to do is this:
I have a class PayloadTester, which like this
public class PayloadTester{
public void test(List<String> list){
// logic
}
}
From my main method I am using reflection apis to get test(List<String> list) method and from this method its parameters like this,
Method method = tester.getClass().getMethod("test", List.class);
Parameter[] params = method.getParameters();
// extracting param from this Parameter array.
I want to use this param which is List<String> and create a function/class get output as java.lang.String.
Because Spring also does something like this where we only pass parameter and payload is mapped to them. How can I achieve that.