1

I am trying to create a custom search for my users repository. I have a custom restcontroller for it

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

The first issue is that I get a 404 when trying to call /users/search/getAvatar?username=Tailor

The second is that how can I add this to the users search links?

Thank you

user1601401
  • 732
  • 1
  • 12
  • 25
  • try calling `call /users/search/getAvatar/?username=Tailor` instead of `call /users/search/getAvatar?username=Tailor` for your first part. It troubled me few days ago – Abhisek Lamsal Mar 13 '16 at 07:38
  • The issue seems to be the produces = "image/jpeg" If I remove that it works if I add it return 404 strange. Any ideas? – user1601401 Mar 15 '16 at 10:01

1 Answers1

2

To add a search link, you need to extend RepositorySearchesResource as illustrated here:

As pointed out in the comments, be sure to check the domain type so as to add search link only for relevant repository.

Community
  • 1
  • 1
Marc Tarin
  • 3,109
  • 17
  • 49
  • 1
    Thanks that is working but I already implement the resourceProcessor for adding non search link. How can I add both search and entity links? – user1601401 Mar 15 '16 at 13:24
  • Check the [accepted answer from the second link](http://stackoverflow.com/a/32796873/5873923): the class implementing ResourceProcessor<> does not have to be your UserController class. A controller is actually not a resource processor, so it makes sense to implement your resource processors from two separate classes, both distinct from the controller. – Marc Tarin Mar 16 '16 at 10:38