• <legend id='JadDQ'><style id='JadDQ'><dir id='JadDQ'><q id='JadDQ'></q></dir></style></legend>

    <small id='JadDQ'></small><noframes id='JadDQ'>

      • <bdo id='JadDQ'></bdo><ul id='JadDQ'></ul>

      <i id='JadDQ'><tr id='JadDQ'><dt id='JadDQ'><q id='JadDQ'><span id='JadDQ'><b id='JadDQ'><form id='JadDQ'><ins id='JadDQ'></ins><ul id='JadDQ'></ul><sub id='JadDQ'></sub></form><legend id='JadDQ'></legend><bdo id='JadDQ'><pre id='JadDQ'><center id='JadDQ'></center></pre></bdo></b><th id='JadDQ'></th></span></q></dt></tr></i><div id='JadDQ'><tfoot id='JadDQ'></tfoot><dl id='JadDQ'><fieldset id='JadDQ'></fieldset></dl></div>
        <tfoot id='JadDQ'></tfoot>

        Spring Data Rest 将自定义端点添加到特定存储库

        时间:2024-08-23
          <i id='yFFj7'><tr id='yFFj7'><dt id='yFFj7'><q id='yFFj7'><span id='yFFj7'><b id='yFFj7'><form id='yFFj7'><ins id='yFFj7'></ins><ul id='yFFj7'></ul><sub id='yFFj7'></sub></form><legend id='yFFj7'></legend><bdo id='yFFj7'><pre id='yFFj7'><center id='yFFj7'></center></pre></bdo></b><th id='yFFj7'></th></span></q></dt></tr></i><div id='yFFj7'><tfoot id='yFFj7'></tfoot><dl id='yFFj7'><fieldset id='yFFj7'></fieldset></dl></div>
            • <bdo id='yFFj7'></bdo><ul id='yFFj7'></ul>

                  <small id='yFFj7'></small><noframes id='yFFj7'>

                  <tfoot id='yFFj7'></tfoot>
                    <tbody id='yFFj7'></tbody>

                  <legend id='yFFj7'><style id='yFFj7'><dir id='yFFj7'><q id='yFFj7'></q></dir></style></legend>
                  本文介绍了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 将自定义端点添加到特定存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:最新和旧版本冲突的两个 Maven 依赖关系 下一篇:未急切获取 DBRefs(Mongo 文档引用)

                  相关文章

                  • <bdo id='rvUyx'></bdo><ul id='rvUyx'></ul>

                  <small id='rvUyx'></small><noframes id='rvUyx'>

                • <legend id='rvUyx'><style id='rvUyx'><dir id='rvUyx'><q id='rvUyx'></q></dir></style></legend>
                  <tfoot id='rvUyx'></tfoot>

                    1. <i id='rvUyx'><tr id='rvUyx'><dt id='rvUyx'><q id='rvUyx'><span id='rvUyx'><b id='rvUyx'><form id='rvUyx'><ins id='rvUyx'></ins><ul id='rvUyx'></ul><sub id='rvUyx'></sub></form><legend id='rvUyx'></legend><bdo id='rvUyx'><pre id='rvUyx'><center id='rvUyx'></center></pre></bdo></b><th id='rvUyx'></th></span></q></dt></tr></i><div id='rvUyx'><tfoot id='rvUyx'></tfoot><dl id='rvUyx'><fieldset id='rvUyx'></fieldset></dl></div>