github linkedin
Routes with dynamic parameters in Micronaut
2020-05-01

If you want to create a REST controller in Micronaut, which accepts a dynamic amount of parameters, e.g.

curl http://localhost:8080/?foo=1&bar=2&baz=3

where the stuff after the ? is varying, here’s the way to go:

@Controller
class FooController {
    @Get("/{?values*}")
    public void takeAll(@Nullable @QueryValue("values") Map<String, String> values) {
        System.out.println("Received: " + values);
    }
}

Micronaut will map the query parameters to the values map. When running the curl from above, the map contains [foo: 1, bar: 2, baz: 3].

If you need a typesafe client for this, that’s how you do it:

@Client("/")
public interface FooClient {
    @Get("/{?values*}")
    void takeAll(@Nullable @QueryValue("values") Map<String, String> values);
}

Back to posts