【问题标题】:How to add custom search link to Spring Data Rest如何将自定义搜索链接添加到 Spring Data Rest
【发布时间】:2016-03-13 07:10:34
【问题描述】:

我正在尝试为我的用户存储库创建自定义搜索。我有一个自定义的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

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

谢谢

【问题讨论】:

  • 在您的第一部分尝试调用 call /users/search/getAvatar/?username=Tailor 而不是 call /users/search/getAvatar?username=Tailor。几天前困扰我
  • 问题似乎是produces =“image/jpeg”如果我删除它,如果我添加它会返回404奇怪。有什么想法吗?

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


【解决方案1】:

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

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

【讨论】:

  • 感谢它正在工作,但我已经实现了用于添加非搜索链接的资源处理器。如何同时添加搜索和实体链接?
  • 检查accepted answer from the second link:实现 ResourceProcessor 的类不必是您的 UserController 类。控制器实际上不是资源处理器,因此从两个独立的类中实现资源处理器是有意义的,这两个类都与控制器不同。
猜你喜欢
  • 2015-12-23
  • 2017-08-15
  • 2015-12-31
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
  • 2012-08-06
相关资源
最近更新 更多