I have the following bean…
User.class:
@JsonFilter("UserFilter")
public class User {
private int id;
@Size(min = 2, message = "Name should have at least 2 characters")
private String name;
@JsonProperty("Birth Date")
@Past(message = "Birth date should be in the past")
private LocalDate birthDate;
public User(int id, String name, LocalDate birthDate) {
this.id = id;
this.name = name;
this.birthDate = birthDate;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + ''' +
", birth=" + birthDate +
'}';
}
}
and I have the following Data Access Object…
UserDaoService.class:
@Component
public class UserDaoService {
private static List<User> users = new ArrayList<>();
static {
users.add(new User(1, "Alice", LocalDate.now()
.minusYears(18)
.minusMonths(1)));
users.add(new User(2, "Bob", LocalDate.now()
.minusYears(21)
.plusMonths(3)
.plusDays(5)));
users.add(new User(3, "Cathy", LocalDate.now()
.minusDays(7)
.minusYears(25)));
users.add(new User(4, "David", LocalDate.now()
.minusYears(23)));
}
public List<User> findAll() {
return users;
}
public User findById(int id) {
return users.stream()
.filter(user -> user.getId() == id)
.findFirst()
.orElse(null);
}
}
The following is the controller I implemented…
UserController:
@RestController
public class UserController {
private UserDaoService service;
public UserController(UserDaoService service) {
this.service = service;
}
@GetMapping("/users")
public MappingJacksonValue findAllUsers() {
List<User> usersList = service.findAll();
MappingJacksonValue mjv = new MappingJacksonValue(usersList);
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.filterOutAllExcept("name", "Birth Date");
FilterProvider filters = new SimpleFilterProvider().addFilter("UserFilter", filter);
mjv.setFilters(filters);
return mjv;
}
@GetMapping("/users/{id}")
public EntityModel<User> findUserById(@PathVariable int id) {
User usr = service.findById(id);
if (usr == null)
throw new UserNotFoundException("id:" + id);
EntityModel<User> model = EntityModel.of(usr);
WebMvcLinkBuilder link = WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(this.getClass()).findAllUsers());
model.add(link.withRel("all-users"));
return model;
}
}
In the controller I am trying to filter out the Users’ names when I return a list of user. I dont want to filter out anything when I try to find a user by a particular ID, though I do however want to add a link to the findAllUsers page.
When I make a GET request to localhost:8080/users/3 with my current implementation I get the following from the stack trace:
HttpMessageConversionException: type definition error: [simple type, class SpringBootFramwork.RESTAPI.beans.User]
caused by InvalidDefinitionException: Cannot resolve PropertyFilter with id 'UserFilter'; no FilterProvider configured (through reference chain: org.springframework.hateoas.EntityModel["content"])
I have no idea why or how to fix this.
Expected output for GET request to localhost:8080/users/3 was:
{
"id": 3,
"name": "Cathy",
"Birth Date": "1999-04-19",
"_links": {
"all-users": {
"href": "http://localhost:8080/users"
}
}
}
Sihle Calana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.