I’m building a controller which takes a request from a third party service. This service have request 5 params which I need to bind to a Message class.
Say, I in request, I’m getting
?a=x&b=y&c=z&d=w&e=k&f=t
The Message class is
public class Message{
String a;
String b;
String c;
String d;
String e;
String f;
public Message(String a, String b, String c, String d, String e, String f){
this.a=a;this.b=b;this.c=c;this.d=d;this.e=e;this.f=f;
}
....// along with getters and setters
}
One option is to use @RequestParam in the method controller, but then I would have to pass all the parameters and then instantiate the Message object manually. I don’t want to do that because the parameter count is too large.
Can this be done using init binder/web data binder? and how?
You don’t need to do anything special to make this work, just declare a
Messageparameter to your controller method:Spring will bind each parameter to a property on
Message, where it can find one. IfMessagehas getters and setters (and a default constructor), it will just work. If you want to use a non-default constructor, or direct field injection, you’ll have to do more config work.