如何删除额外的字段&decormentClass'在Spring数据预测的JSON响应中?

时间:2016-09-10 19:18:42

标签: java spring spring-mvc spring-boot spring-data

您好我使用Spring Data Projection来简化原始对象'公告'。我为我需要的字段创建了get-methods接口:

    public interface SimplifiedAnnouncement{

    String getTitle();

    Integer getPrice();

    String getPlace();
}

然后我只在存储库

中使用它
public interface AnnouncementRepository extends JpaRepository<Announcement,Long> {

     @Query("SELECT a.id AS id, a.title AS title, a.price AS price, a.place AS place FROM Announcement a")
        Page<SimplifiedAnnouncement> getAllSimplifiedAnnouncements(Pageable pageable);
}

REST方法

@RequestMapping(value = "/announcements",
        method = RequestMethod.GET,
        params = "places",
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<List<SimplifiedAnnouncement>> getAllAnnouncements()
        throws URISyntaxException {
        log.debug("REST request to get a page of Announcements");
        Page<SimplifiedAnnouncement> page = announcementRepository.getAllSimplifiedAnnouncements(new PageRequest(0, 100000));
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/announcements");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

正如您在此处所看到的,我有意想不到的字段 decoratedClass 。我发现这个字段来自Proxy class screen,并且有一个标志isMarkedIgnored = false screen。为什么这个领域在这里以及我如何解决它?

{
  "decoratedClass" : "java.util.HashMap",
  "title" : "Tilte",
  "place" : "efwef",
  "price" : 1
}

感谢关注开发人员。

2 个答案:

答案 0 :(得分:3)

这是Spring 4.3中略有改变的代理行为的副作用。即将推出的bugfix版本将发布an already fixed ticket for Spring Data Commons

在此期间,您可以在投影界面上重新声明Class<?> getDecoratedClass(),并使用@JsonIgnore对其进行注释。

答案 1 :(得分:0)

问题可能源于在ResponseEntity中包装Page响应。

您是否尝试输出响应,以检查序列化中的错误?

@RequestMapping(value = "/announcements",
    method = RequestMethod.GET,
    params = "places",
    produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody // <- this makes sure to return the return object
@Timed
public Page<SimplifiedAnnouncement> getAllAnnouncements()
    throws URISyntaxException {
    log.debug("REST request to get a page of Announcements");
    Page<SimplifiedAnnouncement> page = announcementRepository.getAllSimplifiedAnnouncements(new PageRequest(0, 100000));
    return page;
}