4

Assume that I have want to capture a bunch of request parameters as one object like this:

@GetMapping("/")
public List<Item> filterItems(@Valid Filter filter){}

and the Filter class looks like this:

class Filter {
        public String status;
        public String start;
        public String end;
    }

Now in the API the request parameter name is state instead of status like this ?state=A&start=1&end=2. How do I make these request parameters to map to my Filter object without having to rename status? I know if I have @RequestParam("state") String status it would work but I want it to be part of the request object.

I tried adding @JsonProperty('state') on the field but it didn't work.

Koitoer
  • 18,778
  • 7
  • 63
  • 86
danial
  • 4,058
  • 2
  • 32
  • 39
  • Why you don't post the field in the request body, this will work adding @RequestBody also `JsonProperty` only will work on json if you post the entire object in the request. – Koitoer Nov 29 '17 at 21:42
  • I cannot change the API so it has to be in the request parameters. So there is no way to map it to another name other than have it as individual `@RequestParam` argument? – danial Nov 29 '17 at 21:59
  • So you cannot change Filter class and the calling code which is passing queryparam as state instead of status? – Amit K Bist Nov 29 '17 at 22:28
  • 1
    Also you can check https://stackoverflow.com/questions/8986593/how-to-customize-parameter-names-when-binding-spring-mvc-command-objects, focus on the jkee's answer – Amit K Bist Nov 29 '17 at 22:32

0 Answers0