jackson - do not serialize lazy objects

2019-01-23 18:16发布

问题:

I have an entity:

@Entity
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column
    private String title;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = ("movie"),cascade = CascadeType.ALL)
    private List<Genre> genre;

}

Then I have a controller whose purpose is to retrieve books, my problem is that, the genre field is being included in the json response of my controller. Any way I can exclude those fields that are lazy loaded when jackson serializes the object?

This is the configuration of my ObjectMapper:

Hibernate4Module hm = new Hibernate4Module();
hm.configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, false);
registerModule(hm);
configure(SerializationFeature.INDENT_OUTPUT, true);

Thanks!

I can't mark it as JsonIgnore, as it will be forever out of the serialization box. There will be times where I will need to retrieve the genres along with the book, and by then I will use "fetch join" on my query so it will not be null.

回答1:

You can do this with the Jackson @JsonInclude annotation.

According to the latest version's javadoc (2.4 right now) you can specify with a simple annotation if to include or not the annotated property if the field value is null or empty.

By default, it's JsonInclude.Include.ALWAYS, and this means that even if your lazily not-loaded values are null, Jackson does include the property.

Specifying to don't include empty or null values can significantly reduce the size of the JSON response, with all the benefits included..

If you want to change this behavior, you can add the annotation at class-level or single property/getterMethod level.

Try to add the following annotations to the fields you don't want to include if empty:

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@OneToMany(fetch = FetchType.LAZY, mappedBy = ("movie"),cascade = CascadeType.ALL)
private List<Genre> genre;


回答2:

You can use Jackson's JSON Filter Feature:

@Entity
@JsonFilter("Book") 
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column
    private String title;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = ("movie"),cascade = CascadeType.ALL)
    private List<Genre> genre;
} 

@Entity
@JsonFilter("Genre")
public class Genre {
   ...
}

Then in the Controller you specify what to filter:

@Controller
public class BookController {
      @Autowired
      private ObjectMapper objectMapper;

      @Autowird
      private BookRepository bookRepository;

       @RequestMapping(value = "/book", method = RequestMethod.GET, produces =  "application/json")
       @ResponseBody
       public ResponseEntity<String> getBooks() {

          final List<Book> books = booksRepository.findAll();
          final SimpleFilterProvider filter = new SimpleFilterProvider();
          filter.addFilter("Book", SimpleFilterProvider.serializeAllExcept("Genre");
          return new ResponseEntity<>(objectMapper.writer(filter).writeValueAsString(books), HttpStatus.OK)
       }

}

In this way, you can control when you want to filter the lazy relation at runtime



回答3:

Maybe this is related to a known issue about lazy loading.

I don't use jackson-datatype-hibernate, but what I've done to solve the same problem is to get the persistent collection out of the picture by using a DTO instead of serializing a Hibernate object directly. Tools like Dozer can help you out with that. Alternatively, there's a small utility I wrote to do mappings like this.

If you just want to experiment with what a DTO could look like, you can replace the unloaded persistent collection with a regular empty collection, like books.setGenre(new ArrayList<>()); Unfortunately I don't know of a way to tell if a lazily loaded object has been loaded or not, so you can't do this reassignment automatically. The places where you replace persistent collections would need to be determined by you on a case by case basis.