I'm really stuck into trying to map my QueryString parameters into my Spring JavaBean Command object here, and I couldn't find an answer to my question so far.
I'm using jQuery Datatables plugin with server side processing so that each action in my datatable, triggers an AJAX request to my Spring application.
This is the parameters the Datatable plugin is sending to my Rest service:
http://localhost:8080/relatorios/produtos-source?draw=2&columns[0][data]=nome&columns[0][name]=&columns[0][searchable]=true&columns[0][orderable]=true&columns[0][search][value]=&columns[0][search][regex]=false&columns[1][data]=nomesAlternativos&columns[1][name]=&columns[1][searchable]=true&columns[1][orderable]=true&columns[1][search][value]=&columns[1][search][regex]=false&order[0][column]=2&order[0][dir]=asc&start=0&length=10&search[value]=ss&search[regex]=false&_=1400248561282
This is how I am receiving it in my Spring Controller:
@RequestMapping(value = "/produtos-source", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ProdutoTable dataTableRequest(@ModelAttribute DataTableCriterias criterias) {
...
}
And finally, this is my DataTableCriterias JavaBean (indicated by @ModelAttribute):
public class DataTableCriterias {
private int draw;
private int start;
private int length;
private Map<SearchCriterias, String> search;
private List<Map<OrderCriterias, String>> order;
private List<Column> columns;
public enum SearchCriterias {
value,
regex
}
public enum OrderCriterias {
column,
dir
}
public class Column {
private String data;
private String name;
private boolean searchable;
private boolean orderable;
private Map<SearchCriterias, String> search;
}
}
(get / setters ommited)
This almost works perfectly! If I remove this line:
private List<Column> columns;
then Spring automagically populates my DataTableCriterias bean from the query string (but of course, I don't get the columns property mapped)
But with this line added, I get this error:
2014-05-16 17:20:16.605 ERROR 2368 --- [tomcat-http--99] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/relatorios] threw exception [Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'columns[0][data]' of bean class [com.bergermobile.rest.domain.DataTableCriterias]: Illegal attempt to get property 'columns' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'columns' of bean class [com.bergermobile.rest.domain.DataTableCriterias]: Could not instantiate property type [com.bergermobile.rest.domain.DataTableCriterias$Column] to auto-grow nested property path: java.lang.InstantiationException: com.bergermobile.rest.domain.DataTableCriterias$Column] with root cause
org.springframework.beans.NullValueInNestedPathException: Invalid property 'columns' of bean class [com.bergermobile.rest.domain.DataTableCriterias]: Could not instantiate property type [com.bergermobile.rest.domain.DataTableCriterias$Column] to auto-grow nested property path: java.lang.InstantiationException: com.bergermobile.rest.domain.DataTableCriterias$Column
at org.springframework.beans.BeanWrapperImpl.newValue(BeanWrapperImpl.java:651)
As I see it, the main problem is that the 'column' parameter sent by the Datatable plugin is a bidimensional and sometimes also a tridimensional array:
columns[0][data]=nome
columns[0][search][regex]=false
So, it looks like my Bean was correct, but I get this error and now I'm stuck.
Any ideas?
Thanks a lot!