我有一个ViewModel
并根据this post of spring.io在某些字段中添加json
注释,如下所示:
public class SurveyViewModel{
@JsonView(View.Summary.class)
private Long id;
@JsonView(View.Summary.class)
private String title;
private String description;
private String endDate;
//Here is getters & setters
}
当我从REST Controller
检索数据时,如果我的方法的返回类型是List
,则json-result就可以了:
@RequestMapping(value = "/getAllGrid", method = RequestMethod.GET)
@JsonView(View.Summary.class)
@ResponseBody
public List<SurveyViewModel> getAll{
return ModelMapper.mapList(iSurveyService.getAll(), SurveyViewModel.class);
}
但是当我在我的结果上应用分页时将此方法的返回类型更改为QueryResult
,json-result为空:
@RequestMapping(value = "/getAllGrid", method = RequestMethod.GET)
@JsonView(View.Summary.class)
@ResponseBody
public QueryResult<SurveyViewModel> search(String order, int pageNumber, int pageSize) {
return ModelMapper.mapQueryResult(iSurveyService.getAllGrid(new SearchOption("", order, pageNumber, pageSize)), SurveyViewModel.class);
}
当我使用上面的方法对我的结果应用分页时,pageNumber, totalRecords & pageSize
添加到我的json结果,原始数据添加到entityList
作为内部json对象,这是json中的QueryResult
格式不应用@JsonView
:
{
"pageNumber" : 0,
"totalRecords" : 1,
"pageSize" : 10,
"entityList" : [{
"id" : 1,
"title" : "survey1",
"description" : "...",
"endDate" : "2017/01/18",
},{
"id" : 2,
"title" : "survey2",
"description" : "...",
"endDate" : "2016/11/18",
}]
}
现在我的问题是,有没有办法将@JsonView
放在带有QueryResult
returnType的Controller方法上?
答案 0 :(得分:1)
QueryResult
是我用于分页的对象,我通过将@jsonView
放在QueryResult
类的属性上来解决我的问题,如下所示:
public class QueryResult<T> {
@JsonView(View.Summary.class)
private int pageNumber;
@JsonView(View.Summary.class)
private int totalRecords;
@JsonView(View.Summary.class)
private int pageSize;
@JsonView(View.SummaryWithRecipients.class)
private List<T> entityList = new ArrayList<T>();
//Here is getters & setters
}