【发布时间】:2018-03-17 04:54:59
【问题描述】:
我有一个包含 Spring Boot 1.5.7、Spring Data REST、Hibernate、Spring JPA、Swagger2 的项目。
我有两个这样的豆子:
@Entity
public class TicketBundle extends AbstractEntity {
private static final long serialVersionUID = 404514926837058071L;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Note> notes = new ArrayList<>();
.....
}
和
@Entity
public class Note extends AbstractEntity {
private static final long serialVersionUID = -5062313842902549565L;
@Lob
private String text;
...
}
我正在通过存储库公开我的方法:
@Transactional
@RepositoryRestResource(excerptProjection = TicketBundleProjection.class)
@PreAuthorize("isAuthenticated()")
public interface TicketBundleRepository extends PagingAndSortingRepository<TicketBundle, Long> {
....
}
因此,我大摇大摆地看到了我感兴趣的端点,它需要从特定票证包中加载笔记集合:
现在,我想覆盖默认的 GET /api/v1/ticketBundles/{id}/notes 并将其替换为我在 TicketBundleRepository 中输入的自定义方法:
@Transactional(readOnly = true)
@RestResource(rel = "ticketBundleNotes", path = "/ticketBundles/{id}/notes")
@RequestMapping(method = RequestMethod.GET, path = "/ticketBundles/{id}/notes")
@Query("SELECT n FROM TicketBundle tb JOIN tb.notes n WHERE tb.id=:id ORDER BY n.createdDate DESC,n.id DESC")
public Page<Note> getNotes(@Param("id") long id, Pageable pageable);
这样创建查询非常方便,因为我需要使用Pageable并返回一个Page。不幸的是,我现在有两个问题。
第一个问题
该方法映射到端点/api/v1/ticketBundles/search/ticketBundles/{id}/notes instad of /api/v1/ticketBundles/ticketBundles/{id}/notes
第二个问题 当我从 swagger 调用该方法时,我收到一个 HTTP 404:
请求似乎错误。似乎路径变量不理解:
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/api/v1/ticketBundles/search/ticketBundles/{id}/notes?id=1'
这是来自服务器的响应:
{
"timestamp": "2017-10-05T14:00:35.563+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/v1/ticketBundles/search/ticketBundles/%7Bid%7D/notes"
}
服务器端没有任何错误。
有没有办法覆盖端点GET/api/v1/ticketBundles/{id}/notes 通过Repository 暴露它而不使用自定义控制器(使用它我会失去管理Pageable 的设施)?
此外,我在上面显示的调用中获取 HTTP 404 做错了什么?
【问题讨论】:
标签: spring spring-mvc spring-boot spring-data-rest