Search code examples
kotlinmicronaut

How to set multiple params in this type "Class...parameterTypes" when building Micronaut programmatically routes -io.micronaut.web.router.RouteBuilder


I'm trying to build routes for my service with Micronaut. Following this tutorial the example works fine.

This is my controller:

import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.PathVariable

@Controller
class DemoController {

    @Get
    fun issue(
        @PathVariable a: String): String {
        return "Issue # $a"
    }
}

And this is my route class:

import io.micronaut.context.ExecutionHandleLocator
import io.micronaut.web.router.DefaultRouteBuilder
import io.micronaut.web.router.RouteBuilder
import jakarta.inject.Inject
import jakarta.inject.Singleton

@Singleton
class MyRoutes(executionHandleLocator: ExecutionHandleLocator,
               uriNamingStrategy: RouteBuilder.UriNamingStrategy) :
    DefaultRouteBuilder(executionHandleLocator, uriNamingStrategy) {

    @Inject
    fun issuesRoutes(demoController: DemoController) {
        GET("/issues/show/{number}", demoController, "issue", String::class.java)
    }
}

Everything working fine so far.

The problem is that I have more than one parameter in the endpoint. For example:

@Controller
class DemoController {

    @Get
    fun issue(
        @PathVariable a: String,
        @PathVariable b: String
    ): String {
        return "Issue # $a"
    }
}

In MyRoutes class, on the function issuesRoutes, I need to set the parameterTypes for 2 params now, and I don't know how should I do it. The documentation of RouteBuilder says as follow:

Route the specified URI template to the specified target.
The number of variables in the template should match the number of method arguments
Params:
   uri – The URI
   target – The target
   method – The method
   **parameterTypes – The parameter types for the target method**

Returns:The route

@Override
public UriRoute GET(String uri, Object target, String method, Class... parameterTypes) {
    return buildRoute(HttpMethod.GET, uri, target.getClass(), method, parameterTypes);
}

How could I tell the method the types of my two string params (@PathVariables) in this kind of param the method is expecting (Class... parameterTypes).

The error I get with this configuration is:

Caused by: io.micronaut.web.router.exceptions.RoutingException: No such route: com.example.rest.DemoController.issue


Solution

  • Given a controller:

    @Controller
    class DemoController {
    
        @Get
        fun issue(@PathVariable a: String, @PathVariable b: String) = "Issue # a=$a b=$b"
    }
    

    The route would be:

    @Singleton
    class MyRoutes(executionHandleLocator: ExecutionHandleLocator,
                   uriNamingStrategy: RouteBuilder.UriNamingStrategy) :
        DefaultRouteBuilder(executionHandleLocator, uriNamingStrategy) {
    
        @Inject
        fun issuesRoutes(demoController: DemoController) {
            GET("/issues/show/{a}/{b}", // uri
                demoController,  // target       
                "issue", // method
                String::class.java, String::class.java) //vararg parameterTypes:Class
        }
    }