javaspringspring-mvcquery-by-example

Spring MVC Binding Request parameters to POJO fields


I want a controller with the following mapping (incomplete):

@GetMapping(/searchitems)
public @ResponseBody Page<Item> get(Item probe)

From the Item probe parameter I want to query by example in a repository of items and return the result.

Question:

How can I complete the mapping above for a search URL? As search URL I was thinking something like /searchitems?itemAttributeA=foo&itemAttributeB=bar&...itemAttributeZ=xyz. How can I tell spring to inject the passed request parameters into the Item probe fields with the same names?


Solution

  • Adding @ModelAttribute should bind the individual request parameters into your Item POJO.

    public @ResponseBody Page<Item> get(@ModelAttribute Item probe)