【问题标题】:Spring dynamically select @JsonView with paginationSpring动态选择带有分页的@JsonView
【发布时间】:2018-04-18 09:12:18
【问题描述】:

我想根据 URL 参数选择应用于我的数据的 Json 视图。我正在尝试使用@JsonView 注释来实现这一点,并且我尝试了一些解决方案(12)。问题是这些解决方案基于返回 MappingJacksonValue 的控制器操作,但我不能使用它,因为我正在使用分页。

我的行动:

public ResponseEntity<Page<MyEntity>> findAll(
  int viewMode,
  Pageable pageable) {
    result = service.findAll(pageable);

  // Here I would like to apply a Json View to the result set depending 
  // on the variable viewMode

  return new ResponseEntity<Page<MyEntity>>(
    /* Resultset with the selected view applied, and paginated */, 
    HttpStatus.OK
  );
}

【问题讨论】:

    标签: spring spring-mvc spring-boot


    【解决方案1】:

    要使@JsonView 与分页一起使用,您需要在application.properties 中将以下属性设置为true

    spring.jackson.mapper.DEFAULT_VIEW_INCLUSION = true
    

    这将导致映射器也序列化未注释的属性,从而使分页能够工作。

    【讨论】:

    • 感谢您的建议。动态选择要使用的视图怎么样?
    猜你喜欢
    • 2015-05-06
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2012-07-08
    • 2015-07-25
    相关资源
    最近更新 更多