In Spring, is it possible to use @JsonView on URL query parameter objects? I can do it for @RequestBody but we don't have bodies in GET requests. This question is specifically for URL query parameters that have been converted to objects by Spring.
For example, I want to have a controller with this mapping:
@GetMapping("/user")
ResponseEntity<UserDTO> searchUser(@JsonView(value = UserView.Searchable.class) UserDTO userQuery) {
//Do some work here using userQuery object for searching users
return ResponseEntity.ok();
}
UserDTO:
public class UserDTO {
@JsonProperty("id")
@JsonView(UserView.Private.class)
private String id= null;
@JsonView(UserView.Searchable.class)
@JsonProperty("city")
private String city = null;
@JsonProperty("country")
@JsonView(UserView.Searchable.class)
private String country = null;
@JsonProperty("state")
private String state = null;
@JsonProperty("zipCode")
private String zipCode = null;
//More properties and getter/setters...etc
}
So if I wanted to call the endpoint I could create a URL like
localhost:8080//api/user?country=Canada to search for a user in Canada but if I tried localhost:8080//api/user?id=123, the property would be ignored.
EDIT:
I might have rushed this idea. There is no JSON de-serialization from url parameters because they are not JSON. Spring creates the query object from ServletModelAttributeMethodProcessor. Perhaps if I want some custom behavior I need to implement HandlerMethodArgumentResolver and do it myself.
EDIT 2
I'm a bit new to Spring so I have a lot to learn but I think what Ill do is just use @InitBinder to whitelist the fields for binding
@InitBinder
public void setSearchableFields(WebDataBinder binder) {
binder.setAllowedFields(
"city",
"country"
);
}