3

I'm using jQuery DataTables in a Java Spring Boot project. When using DataTables's server-side processing, it sends AJAX request with request parameters like:

?columns[0][data]=0
&columns[0][name]=name
&columns[0][searchable]=true
&columns[0][orderable]=true
&columns[0][search][value]=Tom
&columns[0][search][regex]=false
&columns[1][data]=1
&columns[1][name]=address
&columns[1][searchable]=true
&columns[1][orderable]=true
&columns[1][search][value]=
&columns[1][search][regex]=false

to my server.

How can I convert these request parameters to a Java object for processing? The tutorial simply states that

In most modern server-side scripting environments this data will automatically be available to you as an array.

but I cannot find any way to do this in Java, particularly using Spring Boot's @RequestParameter.

Thank you for your help!

3
  • I suspect you need to create a class that you can deserialize your json object into. Commented Mar 21, 2017 at 2:02
  • That's what I fear, but I was hoping that there would already be a more generic way of deserializing this (maybe using Jackson). Commented Mar 21, 2017 at 2:04
  • You may find this article helpful. Commented Jan 20, 2020 at 12:38

1 Answer 1

4

Create the following classes, ignore the package names //DataTableRequest.java

package com.employee.app.model;

import java.util.*;
import com.fasterxml.jackson.annotation.*;

public class DataTableRequest {
private String draw;
private List<Column> columns;
private List<Order> order;
private String start;
private String length;
private Search search;
private String empty;

@JsonProperty("draw")
public String getDraw() { return draw; }
@JsonProperty("draw")
public void setDraw(String value) { this.draw = value; }

@JsonProperty("columns")
public List<Column> getColumns() { return columns; }
@JsonProperty("columns")
public void setColumns(List<Column> value) { this.columns = value; }

@JsonProperty("order")
public List<Order> getOrder() { return order; }
@JsonProperty("order")
public void setOrder(List<Order> value) { this.order = value; }

@JsonProperty("start")
public String getStart() { return start; }
@JsonProperty("start")
public void setStart(String value) { this.start = value; }

@JsonProperty("length")
public String getLength() { return length; }
@JsonProperty("length")
public void setLength(String value) { this.length = value; }

@JsonProperty("search")
public Search getSearch() { return search; }
@JsonProperty("search")
public void setSearch(Search value) { this.search = value; }

@JsonProperty("_")
public String getEmpty() { return empty; }
@JsonProperty("_")
public void setEmpty(String value) { this.empty = value; }
}

// Column.java

package com.employee.app.model;

import java.util.*;
import com.fasterxml.jackson.annotation.*;

public class Column {
private String data;
private String name;
private String searchable;
private String orderable;
private Search search;

@JsonProperty("data")
public String getData() { return data; }
@JsonProperty("data")
public void setData(String value) { this.data = value; }

@JsonProperty("name")
public String getName() { return name; }
@JsonProperty("name")
public void setName(String value) { this.name = value; }

@JsonProperty("searchable")
public String getSearchable() { return searchable; }
@JsonProperty("searchable")
public void setSearchable(String value) { this.searchable = value; }

@JsonProperty("orderable")
public String getOrderable() { return orderable; }
@JsonProperty("orderable")
public void setOrderable(String value) { this.orderable = value; }

@JsonProperty("search")
public Search getSearch() { return search; }
@JsonProperty("search")
public void setSearch(Search value) { this.search = value; }
}

// Search.java

package com.employee.app.model;

import java.util.*;
import com.fasterxml.jackson.annotation.*;

public class Search {
private String value;
private String regex;

@JsonProperty("value")
public String getValue() { return value; }
@JsonProperty("value")
public void setValue(String value) { this.value = value; }

@JsonProperty("regex")
public String getRegex() { return regex; }
@JsonProperty("regex")
public void setRegex(String value) { this.regex = value; }
}

// Order.java

package com.employee.app.model;

import java.util.*;
import com.fasterxml.jackson.annotation.*;

public class Order {
private String column;
private String dir;

@JsonProperty("column")
public String getColumn() { return column; }
@JsonProperty("column")
public void setColumn(String value) { this.column = value; }

@JsonProperty("dir")
public String getDir() { return dir; }
@JsonProperty("dir")
public void setDir(String value) { this.dir = value; }
}

DataTables by default sends requests as FormData, to make it send that request as Json, do the following.

$(document).ready(function() {
    $('#datatableId').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax":{
        url: "your_processing_endpoint",
        type:"POST",
        contentType:"application/json",
        data:function(d){
        return JSON.stringify(d)
        }
        },
//include other options
    } );
} );

And then in the controller action, assuming your are using Spring boot, do the following

@RequestMapping(value="your_processing_endpoint",method="RequestMethod.POST")
public ResponseEntity<?> processDataTableRequest(@RequestBody DataTableRequest 
datatableRequest){
//you can add your logic here 
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you vaery much, you solved the problem in the best and simple way :-)
The suggested edits queue is currently full, but I found the java.util.* import is not needed on Column, Search and Order. Otherwise, thank you for doing all the leg work on this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.