Issue
When having a bean object for request params in spring: is there a way to define an alias for a bean properties?
@RestController
public class MyServlet {
@GetMapping
public void test(MyReq req) {
}
}
public class MyReq {
@RequestParam("different-name") //this is invalid
private String name;
private int age;
}
Of course @RequestParam
does not work, but is there a similar annotation I could use?
Solution
Request parameter are bind by setters. You can add an extra setter with original parameter name. Something like:
public class MyReq {
private String name;
private int age;
public void setDifferentName(String differentName) {
this.name=differentName;
}
}
NOTE: it will work only if your parameter is camel case like differentName=abc
. Will not work with different-name=abc
.
Answered By - Sergiy Dakhniy
Answer Checked By - Terry (JavaFixing Volunteer)