【问题标题】:Spring Data Rest Add custom endpoint to specific reposiotrySpring Data Rest 将自定义端点添加到特定存储库
【发布时间】:2016-03-06 01:19:24
【问题描述】:

我想将自定义搜索端点添加到我现有的用户存储库。

我的用户存储库如下所示:

@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;
    }
}

这将为用户返回正确的搜索端点:

{
  "_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
    }
  }
}

但也适用于邀请等其他端点:

{
  "_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
    }
  }
}

这怎么能仅限于用户? 谢谢

【问题讨论】:

    标签: java spring spring-data spring-data-rest


    【解决方案1】:

    我假设您的邀请端点也返回 RepositorySearchesResource ?!每当 spring-data-rest 序列化 RepositorySearchesResource 时,都会调用您的 ResourceProcessor。如果您想为用户和邀请提供不同的链接,您有一些选择:

    • 为您的搜索端点使用不同的返回类型,以便您可以有不同的ResourceProcessor 实现
    • 在您的 ResourceProcessor 中添加更多逻辑,以区分您是在邀请还是用户用例中

    【讨论】:

    • 您好,邀请与userRepository接口相同。使用您的第二个建议,我设法通过在我的 RepositorySearchesResource 进程中添加一个 if(resource.getDomainType() == User.class) 来做到这一点。但我一直想知道有没有更好的方法呢?谢谢
    • 嗨,谁能告诉我最新的 Spring 实现中的 ResourceProcessor 替代方案?我知道some of the Class Names are changed。但在那里找不到关于ResourceProcessor 的信息。
    【解决方案2】:

    另一种方法是使用 ResponseBodyAdvice。

    @ControllerAdvice
    public class AllEntityBodyAdvice implements ResponseBodyAdvice {
    
        @Override
        public boolean supports(@NonNull MethodParameter returnType, @NonNull Class converterType) {
            System.out.println("In supports() method of " + getClass().getSimpleName());
            return true;
        }
        @Override
        public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
            System.out.println("In beforeBodyWrite() method of " + getClass().getSimpleName());
    
            if (Objects.equals(request.getMethod(), HttpMethod.GET)) {
                 // do sth
            }
    
            return body;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-29
      • 2018-03-17
      • 2018-10-06
      • 2017-09-19
      • 2017-08-15
      • 1970-01-01
      • 2016-01-21
      • 2016-08-29
      • 2014-02-02
      相关资源
      最近更新 更多