I'm using Jackson in Spring MVC @ResponseBody to generate JSON for jqGrid. I'm using a POJO like this for JSON required by jqGrid:
public class Grid<T> implements Serializable {
private int totalPages;
private int currentPage;
private long totalRecords;
private List<T> listData;
// getter & setter...
}
I'm putting domain model retrieved from Hibernate JPA (maybe a proxy because there is lazy fetching in some attributes) such as:
@Entity @Cacheable
public class Item implements Serializable {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Version
private int version;
@NotBlank
private String name;
@JsonIgnore @Lob @Basic(fetch=FetchType.LAZY)
private byte[] image;
// getter and setter...
}
This is my code in Spring MVC controller:
@RequestMapping(value="listgrid", method=RequestMethod.GET, produces="application/json")
@ResponseBody
public Grid<T> listGrid(@RequestParam(value="page", required=false) Integer page,
@RequestParam(value="rows", required=false) Integer rows,
@RequestParam(value="sidx", required=false) String sidx,
@RequestParam(value="sord", required=false) String sord,
@RequestParam(value="_search", required=false) String search) {
// ...
Grid<T> grid = new Grid<T>();
Page<T> objPage = retrieveData(...);
grid.setListData(objPage.getContent());
grid.setCurrentPage(objPage.getNumber()+1);
grid.setTOtalPages(objPage.getTotalPages());
grid.setTotalRecords(objPage.getTotalElements());
return grid;
}
I've put @JsonIgnore in image attribute, but the resulting JSON will always contains image. How to ignore this attribute?