Search code examples
micronaut

Controller mapping with multiple query optional parameters not working


I want to map a request to a controller that processes URLs like:

/weather?city=London
/weather?city=London&country=GB

Notice city and country are optional parameters.

This simple controller is:

@Controller("/weather")
class WeatherController(...) {

    @Get(produces = [MediaType.APPLICATION_JSON])
    fun getWeather(city: String?, country: String?): Flux<Any> = ...

But there seems to be some mapping problem:

java.lang.NoSuchMethodError: com.codependent.weatherapp.controller.WeatherController.getWeather(Ljava/lang/String;Ljava/lang/String;)Lreactor/core/publisher/Flux;
    at com.codependent.weatherapp.controller.$WeatherControllerDefinition$$exec1.invokeInternal(Unknown Source)
    at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:145)

UPDATE: The project is available on Github.

I've tried several ways to map optional parameters, but none of them worked:

1) Following URI Path Variables documentation http://localhost:8080/weather/test1

@Get(value = "/test1{?city,country,cityIds}", produces = [MediaType.APPLICATION_JSON])
fun getWeather(city: Optional<String>, country: Optional<String>, cityIds: Optional<String>) = "The weather is...".toMono()

2) Just indicating the optional, without the actual request parameter mapping - http://localhost:8080/weather/test2

@Get(value = "/test2", produces = [MediaType.APPLICATION_JSON])
fun getWeather2(city: Optional<String>, country: Optional<String>, cityIds: Optional<String>) = "The weather is...".toMono()

3) The Kotlin way, just expressing the nullability of the params - http://localhost:8080/weather/test3

@Get(value = "/test3", produces = [MediaType.APPLICATION_JSON])
fun getWeather3(city: String?, country: String?, cityIds: String?) = "The weather is...".toMono()

Solution

  • The project is ok, there seemed to be a problem with IntelliJ that wasn't building it properly. I changed the building configuration to delegate the process to Gradle and problem solved.

    I appreciated @James Kleeh support. Enjoy Micronaut!