我正在尝试将自定义方法添加到我的 Spring 数据存储库PersonRepository
如中所述1.3 Spring Data 存储库的自定义实现并通过 REST 公开这些方法。初始代码来自使用 REST 访问 JPA 数据示例,这里是添加/修改类的代码:
interface PersonRepositoryCustom {
List<Person> findByFistName(String name);
}
class PersonRepositoryImpl implements PersonRepositoryCustom, InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
// initialization here
}
@Override
public List<Person> findByFistName(String name) {
// find the list of persons with the given firstname
}
}
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
当我运行应用程序并访问http://localhost:8080/portfolio/search/
,我得到以下响应正文:
{
"_links" : {
"findByLastName" : {
"href" : "http://localhost:8080/people/search/findByLastName{?name}",
"templated" : true
}
}
}
Why findByFirstName
即使在可用的情况下也不会暴露PersonRepository
界面?
另外,有没有办法动态/以编程方式添加要通过 REST 公开的存储库?
这些方法不公开的原因是,您基本上可以自由地在自定义存储库方法中实现您想要的任何内容,因此不可能推断出正确的 HTTP 方法来支持该特定资源。
在你的情况下,使用普通的可能会很好GET
,在其他情况下,它可能必须是POST
因为该方法的执行有副作用。
当前的解决方案是制作一个自定义控制器来调用存储库方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)