Last active
January 13, 2018 11:35
-
-
Save Sam-Kruglov/0f0195beb8174b5e33f12a7b32b24659 to your computer and use it in GitHub Desktop.
JsonFilter with broken links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @RepositoryRestController | |
| public class UserController { | |
| private final UserRepository userRepository; | |
| private final RepositoryEntityLinks links; | |
| public UserController(final UserRepository userRepository, | |
| final RepositoryEntityLinks links) { | |
| this.userRepository = userRepository; | |
| this.links = links; | |
| } | |
| @GetMapping(value = "users/{id}", params = "fields") | |
| public ResponseEntity<MappingJacksonValue> getUser(@PathVariable("id") Long id, | |
| @RequestParam(value = "fields") String fields, | |
| HttpServletRequest request) { | |
| Resource<User> userResource = new Resource<>(userRepository.findOne(id)); | |
| userResource.add(new Link(request.getRequestURL() + "?fields=" + fields).withSelfRel()); | |
| userResource.add(links.linkToSingleResource(userResource.getContent()).withRel("user")); | |
| userResource.add(links.linkToSingleResource(userResource.getContent().getGroup()).withRel("group")); | |
| MappingJacksonValue wrapper = new MappingJacksonValue(userResource); | |
| wrapper.setFilters(new SimpleFilterProvider() | |
| .addFilter("userFilter", | |
| SimpleBeanPropertyFilter.filterOutAllExcept(fields.split(",")))); | |
| return ResponseEntity.ok(wrapper); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment