Search code examples
springspring-mvcspring-bootspring-cloudspring-cloud-feign

Spring Cloud OpenFeign Failed to Create Dynamic Query Parameters


Spring cloud openFeign can't create dynamic query parameters. It throws below exception because SpringMvcContract tries to find the RequestParam value attribute which doesn't exist.

java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0


@RequestMapping(method = RequestMethod.GET, value = "/orders")
Pageable<Order> searchOrder2(@RequestParam CustomObject customObject);

I tried using @QueryMap instead of @RequestParam but @QueryMap does not generate query parameters.

Btw @RequestParam Map<String, Object> params method parameter works fine to generate a dynamic query parameter.

But I want to use a custom object in which the feign client can generate dynamic query parameters from the object's attributes.


Solution

  • From Spring Cloud OpenFeign Docs:

    Spring Cloud OpenFeign provides an equivalent @SpringQueryMap annotation, which is used to annotate a POJO or Map parameter as a query parameter map

    So your code should be:

    @RequestMapping(method = RequestMethod.GET, value = "/orders")
    Pageable<Order> searchOrder2(@SpringQueryMap @ModelAttribute CustomObject customObject);