I just found a related SO question, and one of the simpler "hack" solutions is to add a setter.
@GetMapping("/modeltest")
public void testModel(@ModelAttribute Test test) {
log.info("{}", test);
}
@Data
public static class Test {
private String keyword;
private String xqId;
public void setXq_id(String xqId) { //setxq_id also seems to work
this.xqId = xqId;
}
}
upon calling curl -X GET 'http://localhost:8080/modeltest?xq_id=1' --header 'Content-Type: application/json'
, it logged Test(keyword=null, xqId=1)
. Though this may not work for every case (i.e. if the request param is kebab case xq-id
).
source:
How to customize parameter names when binding Spring MVC command objects?