Created
July 28, 2020 08:26
-
-
Save iamvickyav/c66e3120c5b0086ef23ac16c6f629196 to your computer and use it in GitHub Desktop.
This file contains 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
@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