When making controller for spring mvc application I have to specify
a new method every time I want to pass a new class instances from spring forms:
@RequestMapping(method=RequestMethod.POST)
public String mainPost(@Valid MyClass myClass,
BindingResult result, Model model) {
// do the same stuff each time here
}
So I need to write a method for every possible MyClass in my application
that can be passed to controller, but all these methods do absolutely the same stuff
(they check for errors and pass this object to service layer).
How can I merge all this methods in one?
I believe that there is some solution similar to the following (that doesn’t work):
@RequestMapping(method=RequestMethod.POST)
public <T> String mainPost(@Valid T myObject,
BindingResult result, Model model) {
// check errors and pass the object myObject to a service layer
}
You cannot have generic controller methods, but you can create generic controller: