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

Spring Data Rest Add custom endpoint to specific reposiotry

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

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

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

这 returns 用户的正确搜索端点:

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

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

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

  • 为您的搜索端点使用不同的 return 类型,这样您就可以有不同的 ResourceProcessor 实现方式
  • 在您的 ResourceProcessor 中加入更多逻辑以区分您是在邀请中还是用户用例中

另一种方法是使用 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;
    }
}