我在 Spring MVC @ResponseBody 中使用 Jackson 为 jqGrid 生成 JSON。我正在为 jqGrid 所需的 JSON 使用这样的 POJO:
public class Grid<T> implements Serializable {
private int totalPages;
private int currentPage;
private long totalRecords;
private List<T> listData;
// getter & setter...
}
我将从 Hibernate JPA 检索到的域模型(可能是代理,因为某些属性中存在延迟获取),例如:
@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...
}
这是我在 Spring MVC 控制器中的代码:
@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;
}
我已将@JsonIgnore 放在图像属性中,但生成的 JSON 将始终包含图像。如何忽略此属性?