Java 如何删除额外字段';装饰类';在Spring数据投影的JSON响应中?
您好,我正在使用简化原始对象“公告”。我为我需要的字段创建了带有get方法的接口:Java 如何删除额外字段';装饰类';在Spring数据投影的JSON响应中?,java,spring,spring-mvc,spring-boot,spring-data,Java,Spring,Spring Mvc,Spring Boot,Spring Data,您好,我正在使用简化原始对象“公告”。我为我需要的字段创建了带有get方法的接口: public interface SimplifiedAnnouncement{ String getTitle(); Integer getPrice(); String getPlace(); } 然后我就在存储库中使用它了: public interface AnnouncementRepository extends JpaRepository<Announc
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);
}
@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);
}
感谢您的关注,亲爱的开发人员。问题可能源于将
页面
响应包装在一个ResponseEntity中
您是否尝试只输出响应,以检查序列化中的错误
@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;
}
@RequestMapping(value=“/announcements”,
method=RequestMethod.GET,
params=“places”,
products=MediaType.APPLICATION\u JSON\u值)
@ResponseBody/这是Spring 4.3中代理行为稍有改变的副作用。即将发布的bugfix版本也将发布
同时,您可以在投影界面上重新声明类getDecoratedClass()
,并使用@JsonIgnore
对其进行注释,因为您没有使用分页。您是否尝试将存储库中的响应类型从页面
更改为列表
,如Joey中所定义,我正在使用REST服务中的分页。你可以在上面看到。我得到了这个错误<代码>无法在此ManagedType上找到具有给定名称[decoratedClass]的属性
。只要在投影界面上声明就足够了吗?
@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;
}