为什么杰克逊注释被列入类列表?

时间:2012-09-21 09:35:51

标签: spring-mvc jackson

我在Spring MVC @ResponseBody中使用Jackson为jqGrid生成JSON。我正在使用这样的POJO来获取jqGrid所需的JSON:

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将始终包含图像。如何忽略这个属性?

1 个答案:

答案 0 :(得分:0)

你在吸气器上试过@JsonIgnore吗?