如何向Spring Data Rest添加自定义搜索链接

时间:2016-03-13 07:10:35

标签: spring spring-data 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时收到404?username = Tailor

第二个是如何将其添加到用户搜索链接?

谢谢

1 个答案:

答案 0 :(得分:2)

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

正如评论中所指出的,请务必检查域类型,以便仅为相关存储库添加搜索链接。