1

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?

steven
  • 87
  • 5
  • Did you see [this question](http://stackoverflow.com/questions/8421990/jackson-1-9-2-getting-jsonignore-to-work-deserialization-with-mixin-annotati)? What version of Jackson and Spring do you use? – Grzegorz Rożniecki Sep 21 '12 at 09:48
  • Thanks for the link. From what I understand, the question is about deserialization but I'm trying to ignore a field during serialization (from java class to JSON). I'm using jackson-mapper-lgpl 1.9.9 and spring-webmvc 3.1.1. – steven Sep 21 '12 at 10:16

1 Answers1

0

Have you tried @JsonIgnore on the getter ?

MikePatel
  • 2,593
  • 2
  • 24
  • 38
  • It turns out that the class contains some operation methods such as `getSmallImage()` and `getCustomSizeImage()`. After adding @JsonIgnore to those operation methods, my code is working now. Thank :) – steven Sep 21 '12 at 11:43