I have a data access layer made with Spring-Data. I\'m now creating a web application on top of it. This one controller method should return a Spring-Data Page formatted as
Oliver, your answer is great and I mark it as answer. Here just for completeness what I came up with for the mean time which might be useful for someone else.
I use JQuery Datatables as my grid/table widget. It sends very specific parameter to server and excepts a very specific response: see http://datatables.net/usage/server-side.
To achieve this is created a custom helper object reflecting what datatables expects. Note that getter and setter must be named like they are else the produced json is wrong (case sensitive property names and datatables uses this "pseudo Hungarian notation"...).
public class JQueryDatatablesPage implements java.io.Serializable {
private final int iTotalRecords;
private final int iTotalDisplayRecords;
private final String sEcho;
private final List aaData;
public JQueryDatatablesPage(final List pageContent,
final int iTotalRecords,
final int iTotalDisplayRecords,
final String sEcho){
this.aaData = pageContent;
this.iTotalRecords = iTotalRecords;
this.iTotalDisplayRecords = iTotalDisplayRecords;
this.sEcho = sEcho;
}
public int getiTotalRecords(){
return this.iTotalRecords;
}
public int getiTotalDisplayRecords(){
return this.iTotalDisplayRecords;
}
public String getsEcho(){
return this.sEcho;
}
public List getaaData(){
return this.aaData;
}
}
The second part is a method in the according controller:
@RequestMapping(value = "/search", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody String search (
@RequestParam int iDisplayStart,
@RequestParam int iDisplayLength,
@RequestParam int sEcho, // for datatables draw count
@RequestParam String search) throws IOException {
int pageNumber = (iDisplayStart + 1) / iDisplayLength;
PageRequest pageable = new PageRequest(pageNumber, iDisplayLength);
Page page = compoundService.myCustomSearchMethod(search, pageable);
int iTotalRecords = (int) (int) page.getTotalElements();
int iTotalDisplayRecords = page.getTotalPages() * iDisplayLength;
JQueryDatatablesPage dtPage = new JQueryDatatablesPage<>(
page.getContent(), iTotalRecords, iTotalDisplayRecords,
Integer.toString(sEcho));
String result = toJson(dtPage);
return result;
}
private String toJson(JQueryDatatablesPage> dt) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Hibernate4Module());
return mapper.writeValueAsString(dt);
}
compoundService
is backed by a Spring-Data repository. It manages transactions and method level security. toJSON()
method uses Jackson 2.0 and you need to register the appropriate module to the mapper, in my case for hibernate 4.
In case you have bidirectional relationships, you need to annotate all your entity classes with
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="jsonId")
This enables Jackson 2.0 to serialize circular dependencies (was not possible in earlier version and requires that your entities are annotated).
You will need to add following dependencies:
com.fasterxml.jackson.core
jackson-core
2.2.1
com.fasterxml.jackson.datatype
jackson-datatype-hibernate4
2.2.1
com.fasterxml.jackson.core
jackson-annotations
2.2.1
jar