I'm trying to map certain values from request query parameters to POJO parameters as the following. This is the POJO:
import lombok.*;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Map;
@Data
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Person {
@NotNull(message = "personId cannot be null")
@Min(value = 0)
private Long personId;
@NotNull(message = "from date cannot be null")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime from;
@NotNull(message = "to date cannot be null")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private LocalDateTime to;
private Map<String, String> filters;
}
And this is the controller:
@GetMapping(path = "/persons")
public ResponseEntity<Integer> getPersons(@Valid final Person personRequest) throws Exception {
return ResponseEntity.ok(personService.getPersonsCount(personRequest));
}
I want to map request query parameters to the attributes of this pojo. This is the expected request:
{application_url}/persons?personId=12&from=2017-03-22T00:00:00&to=2019-03-22T00:00:00&country=UK&company=xyz
I want to map personId, from, to
to corresponding attributes in POJO and map the rest of query parameters nationality, company
to the filters map. In other words personId, to, from
are only static in the request and the rest of the parameters may vary, we can have salary=1000&minAage=31
instead of country=UK&company=xyz
so I want to map the rest of the parameters to the filters
map.
Is there a way to achieve so?