尝试使用 Spring HATEOS link 到 JPA 存储库查询方法并获取 IllegalArgumentException:'uriTemplate' 不能为空
Trying to link to JPA Repository query methods with Spring HATEOS and get IllegalArgumentException: 'uriTemplate' must not be null
在我的应用程序中,我在 Things 和 Stuff 之间有一个外键关系,其中给定的 Thing 可能包含数百个 Stuff,我正在使用 Spring Data JPA 来公开 Thing 和 Stuff 存储库。
我想显示与用户选择的事物关联的所有 Stuff,但由于 return 的大小,我想分页 Stuff 结果。
搜索显示无法将分页功能添加到来自事物 return 的嵌入式内容 links,因此下面的 link 为 returned从我的事物存储库中永远无法分页:
"stuff": {
"href": "http://localhost:8080/api/things/1/stuff"
}
所以我在我的 Stuff 存储库中添加了一个自定义方法以通过事物 Id 获取所有 Stuff,并且在直接调用时工作正常。
我想添加一个 Link 到指向自定义搜索方法的事物资源 return 以获取所有关联的东西,但是当我使用 ControllerLinkBuilder.linkTo() 方法时它失败了
java.lang.IllegalArgumentException: 'uriTemplate' must not be null
at org.springframework.util.Assert.hasText(Assert.java:181) ~[spring-core-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.web.util.UriTemplate.<init>(UriTemplate.java:61) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
资料库:
public interface StuffRepo extends JpaRepository<Stuff, Long> {
Page<Stuff> findByThingId(@Param("thingId") Long thingId, Pageable pageable);
}
配置:
@Bean
ThingProcessor getThingProcessor()
{
return new ThingProcessor();
}
public static class ThingProcessor implements ResourceProcessor<Resource<Thing>>{
@Override
public Resource<Thing> process(Resource<Thing> resource) {
ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(StuffRepo.class).findByThingId(resource.getContent().id, null));
return resource;
}
}
我是否遗漏了一些注释或配置?我已经尝试使用 @RestResource 注释 Repo 和方法,但没有任何区别。还有没有更好的方法来获取子对象的分页结果?
我使用 RepositoryEntityLinks
class 解决了这个问题,它为您提供了一个 Link 对象的列表,然后我从那里找到了我需要使用 link.getRel()
的对象
在我的应用程序中,我在 Things 和 Stuff 之间有一个外键关系,其中给定的 Thing 可能包含数百个 Stuff,我正在使用 Spring Data JPA 来公开 Thing 和 Stuff 存储库。
我想显示与用户选择的事物关联的所有 Stuff,但由于 return 的大小,我想分页 Stuff 结果。
搜索显示无法将分页功能添加到来自事物 return 的嵌入式内容 links,因此下面的 link 为 returned从我的事物存储库中永远无法分页:
"stuff": {
"href": "http://localhost:8080/api/things/1/stuff"
}
所以我在我的 Stuff 存储库中添加了一个自定义方法以通过事物 Id 获取所有 Stuff,并且在直接调用时工作正常。
我想添加一个 Link 到指向自定义搜索方法的事物资源 return 以获取所有关联的东西,但是当我使用 ControllerLinkBuilder.linkTo() 方法时它失败了
java.lang.IllegalArgumentException: 'uriTemplate' must not be null
at org.springframework.util.Assert.hasText(Assert.java:181) ~[spring-core-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.web.util.UriTemplate.<init>(UriTemplate.java:61) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
资料库:
public interface StuffRepo extends JpaRepository<Stuff, Long> {
Page<Stuff> findByThingId(@Param("thingId") Long thingId, Pageable pageable);
}
配置:
@Bean
ThingProcessor getThingProcessor()
{
return new ThingProcessor();
}
public static class ThingProcessor implements ResourceProcessor<Resource<Thing>>{
@Override
public Resource<Thing> process(Resource<Thing> resource) {
ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(StuffRepo.class).findByThingId(resource.getContent().id, null));
return resource;
}
}
我是否遗漏了一些注释或配置?我已经尝试使用 @RestResource 注释 Repo 和方法,但没有任何区别。还有没有更好的方法来获取子对象的分页结果?
我使用 RepositoryEntityLinks
class 解决了这个问题,它为您提供了一个 Link 对象的列表,然后我从那里找到了我需要使用 link.getRel()
的对象