Can't map a Query String parameters to my Java

2019-01-22 15:29发布

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!

3条回答
孤傲高冷的网名
2楼-- · 2019-01-22 15:58

I know I'm late to the party, but this: https://github.com/darrachequesne/spring-data-jpa-datatables

is a lovely alternative to read your datatables input elegantly. Even if you dont want to use JPA, you can simply use the DataTablesInput and DataTablesOutput classes to read the input correctly. I also included jquery.spring-friendly.min.js which allowed Spring to easily read the variables that came in.

Thanks to https://github.com/darrachequesne for a nice elegant solution.

查看更多
别忘想泡老子
3楼-- · 2019-01-22 16:06

You send columns[0] as parameter, but columns has no entry with the index 0. In fact it has no entry at all as it is null.

You have to instantiate the list in the constructor of DataTableCriterias in order to make this work. And it has to have enough entries for the parameters you send.

查看更多
看我几分像从前
4楼-- · 2019-01-22 16:10

Ok, so I ended up fixing this issue with slightly modifying the parameters sent to the server, to transform the two 3D array of columns into a 2D array. So:

columns[0][search][value]=myvalue
columns[0][search][regex]=false

ended up being:

columns[0][searchValue]=myvalue
columns[0][searchRegex]=false

This is how to do that in Databables:

$('#produtosTable').DataTable({
    serverSide: true,
    ajax: {
        "url": "produtos-source",
        "data": function(data) {
            planify(data);  
        } 
    }
});

function planify(data) {
    for (var i = 0; i < data.columns.length; i++) {
        column = data.columns[i];
        column.searchRegex = column.search.regex;
        column.searchValue = column.search.value;
        delete(column.search);
    }
}

This way I can receive those properties in my Model object using this field:

private List<Map<ColumnCriterias, String>> columns;

Just for reference, here is my Controller:

@RequestMapping(value = "/produtos-source", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ProdutoTable dataTableRequest(@ModelAttribute DataTableCriterias criterias) {
    ProdutoTable produtoTable = produtosService.findProdutos();
    produtoTable.setDraw(criterias.getDraw());
    return produtoTable;
}

and here is final my DataTableCriterias @ModelAttriute:

public class DataTableCriterias {
    private int draw;
    private int start;
    private int length;

    private Map<SearchCriterias, String> search;

    private List<Map<ColumnCriterias, String>> columns;

    private List<Map<OrderCriterias, String>> order;

    public enum SearchCriterias {
        value,
        regex
    }
    public enum OrderCriterias {
        column,
        dir
    }
    public enum ColumnCriterias {
        data,
        name,
        searchable,
        orderable,
        searchValue,
        searchRegex
    }

(get/setters omitted)

查看更多
登录 后发表回答