0

If I use @RequestParam in my controllers, I can name a variable different from the param name, if I set "name" attribute.

public String showSomething(@RequestParam(name = "field_1") String firstField /*other params*/) {
  return "viewName";
}

Can I name a variable different from the param name, if I use complex object?

ComplexObject.java

public class ComplexObject {
  private String firstField; // I want the value of the "field_1" parameter to get here
  private String secondField; // And "field_2" to get here

//getters, setters

}

SimpleController.java

@Controller
public class SimpleController {

  public String showSomething(ComplexObject complexObject) {
     return "viewName";
}

If possible, I would like to know how to do it.

  • 2
    Does this answer your question? [Spring MVC: Complex object as GET @RequestParam](https://stackoverflow.com/questions/16942193/spring-mvc-complex-object-as-get-requestparam) – PatrickChen May 09 '20 at 00:24
  • No. I know how to use complex objects as param, I find alternative for "name" attribute @RequestParam annotation for complex object fields. – nikitoz13066 May 09 '20 at 00:46
  • You could probably do this with a custom binder, but it's _really_ not worth the trouble. The entire point of the Web model is to represent the API between the server and client, and it's best for it to match as closely as possible. – chrylis -cautiouslyoptimistic- May 09 '20 at 02:00

0 Answers0