Spring Data Rest Add custom endpoint to specific reposiotry(Spring Data Rest 将自定义端点添加到特定存储库)
问题描述
我想将自定义搜索端点添加到我现有的用户存储库.
I would like to add a custom search endpoint to my existing user repository.
我的用户存储库如下所示:
My user Repository looks like this:
@RepositoryRestResource(collectionResourceRel="users", path="users")
public interface UserRepository extends PagingAndSortingRepository<User, Long>{
User findByUsername(String username);
}
自定义控制器:
@BasePathAwareController
@RequestMapping("users/search")
public class CustomController implements ResourceProcessor<RepositorySearchesResource>, ResourceAssembler<User, Resource<User>> {
@Autowired
UserRepository userReposiotry;
@Autowired
private EntityLinks entityLinks;
@RequestMapping(value = "findFirst", produces = "application/json")
@ResponseBody
public ResponseEntity<Resource<User>> findFirstUser() {
Resource<User> resource = toResource(userReposiotry.findOne(1L));
return new ResponseEntity<Resource<User>>(resource, HttpStatus.OK);
}
@Override
public RepositorySearchesResource process(RepositorySearchesResource resource) {
LinkBuilder lb = entityLinks.linkFor(User.class, "username");
resource.add(new Link(lb.toString() + "/search/findFirst", "findFirst"));
return resource;
}
@Override
public Resource<User> toResource(User user) {
Resource<User> resource = new Resource<User>(user);
return resource;
}
}
这将为用户返回正确的搜索端点:
This returns the correct search endpoint for the users:
{
"_links": {
"findByUsername": {
"href": "http://localhost:8080/api/users/search/findByUsername"
},
"self": {
"href": "http://localhost:8080/api/users/search"
},
"findFirst": {
"href": "http://localhost:8080/api/users/search/findFirst",
"templated": true
}
}
}
但也适用于邀请等其他端点:
But also for other endpoints like Invites:
{
"_links": {
"findUserByInvite": {
"href": "http://localhost:8080/api/invites/search/findUserByInvite"
},
"self": {
"href": "http://localhost:8080/api/invites/search"
},
"findFirst": {
"href": "http://localhost:8080/api/invites/search/findFirst",
"templated": true
}
}
}
这怎么能仅限于用户?谢谢
How can this be restricted to the users only? Thanks
推荐答案
我假设您的邀请端点也返回一个 RepositorySearchesResource
?!每当 spring-data-rest 序列化 RepositorySearchesResource
时,都会调用您的 ResourceProcessor
.如果您想为用户和邀请提供不同的链接,您有一些选择:
I assume your invites endpoint also returns a RepositorySearchesResource
?! Your ResourceProcessor
is invoked whenever spring-data-rest serializes a RepositorySearchesResource
. If you want different links for users and invites you have some alternatives:
- 为您的搜索端点使用不同的返回类型,以便您可以有不同的
ResourceProcessor
实现 - 在您的
ResourceProcessor
中添加更多逻辑,以区分您是在邀请还是用户用例中
- use different return types for your search endpoints so you can have different
ResourceProcessor
implementations - put more logic inside your
ResourceProcessor
to differentiate if you are in your invites or users use case
这篇关于Spring Data Rest 将自定义端点添加到特定存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Spring Data Rest 将自定义端点添加到特定存储库


基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01