Skip to content

Instantly share code, notes, and snippets.

@iamvickyav
Created July 28, 2020 08:26
Show Gist options
  • Save iamvickyav/c66e3120c5b0086ef23ac16c6f629196 to your computer and use it in GitHub Desktop.
Save iamvickyav/c66e3120c5b0086ef23ac16c6f629196 to your computer and use it in GitHub Desktop.
@JsonFilter("userFilter")
public class User {
private Integer id;
private String name;
private Date dob;
private String city;
// constructors, getters & setters are ignored
}
@RestController
public class UserController {
@Autowired
UserService userService;
@RequestMapping(value = "/user/{name}", method= RequestMethod.GET)
MappingJacksonValue getUserByName(@PathVariable("name") String name){
SimpleBeanPropertyFilter simpleBeanPropertyFilter =
SimpleBeanPropertyFilter.serializeAllExcept("id", "dob");
FilterProvider filterProvider = new SimpleFilterProvider()
.addFilter("userFilter", simpleBeanPropertyFilter);
User user = userService.getUser(name);
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(user);
mappingJacksonValue.setFilters(filterProvider);
return mappingJacksonValue;
}
@RequestMapping(value = "/users", method= RequestMethod.GET)
MappingJacksonValue getAllUsers(){
SimpleBeanPropertyFilter simpleBeanPropertyFilter =
SimpleBeanPropertyFilter.serializeAllExcept("id", "dob");
FilterProvider filterProvider = new SimpleFilterProvider()
.addFilter("userFilter", simpleBeanPropertyFilter);
List<User> userList = userService.getAllUsers();
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(userList);
mappingJacksonValue.setFilters(filterProvider);
return mappingJacksonValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment