如何将自定义搜索 link 添加到 Spring 数据 Rest

How to add custom search link to Spring Data Rest

我正在尝试为我的用户存储库创建自定义搜索。我有一个自定义的 restcontroller

@BasePathAwareController
@RequestMapping("/users")
@MultipartConfig(fileSizeThreshold = 20971520)
public class UserController implements ResourceProcessor<Resource<User>>,{

    @Autowired
    UserRepository userReposiotry;

    @Autowired
    private EntityLinks entityLinks;


    @RequestMapping(value = "/search/getAvatar", method = RequestMethod.GET, produces = "image/jpg")
    public ResponseEntity<InputStreamResource> downloadImage(@RequestParam("username") String username)
            throws IOException {

        ClassPathResource file = new ClassPathResource("uploads/" + username+ "/avatar.jpg");

        return ResponseEntity
                .ok()
                .contentLength(file.contentLength())
                .contentType(
                        MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }

    @Override
    public Resource<User> process(Resource<User> resource) {
        LinkBuilder lb = entityLinks.linkFor(User.class);
        resource.add(new Link(lb.toString()));

        **// How can I add the search/getAvatar to the user search resource?**

        return resource;
    }
}

第一个问题是我在尝试调用 /users/search/getAvatar?username=Tailor

时收到 404

第二个问题是如何将此添加到用户搜索链接中?

谢谢

要添加搜索 link,您需要扩展 RepositorySearchesResource,如下所示:

正如评论中所指出的,一定要检查域类型以便只为相关存储库添加搜索link。