Created
May 23, 2019 02:39
-
-
Save C06A/e80b783a1d34cc9d80e339da0d7b5b53 to your computer and use it in GitHub Desktop.
Micronaut parameters binding in Kotlin controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.helpchoice.kotlin.hal | |
import io.micronaut.http.MediaType | |
import io.micronaut.http.annotation.Controller | |
import io.micronaut.http.annotation.Get | |
@Controller("/", produces = [MediaType.APPLICATION_HAL_JSON]) | |
class StackSample { | |
// @Get("/repeat/{value}{/stack") | |
// fun dup(value: Number, stack: List<Number>): Map<String, Any?> | |
@Get("/repeat/{value:[0-9]+(\\.[0-9]+)}{/stack:[0-9]+(\\.[0-9]+)?(/[0-9]+(\\.[0-9]+)?)*}/") | |
fun dup(value: String, stack: String?): Map<String, Any?> { | |
val baseHref = "/$value/$value/${stack}" | |
val selfHref = "/repeat/$value/$stack" | |
return mapOf( | |
"_links" to mapOf( | |
"self" to mapOf( | |
"href" to selfHref, | |
"type" to MediaType.APPLICATION_HAL_JSON | |
) | |
), | |
"stack" to stack, | |
"value" to value | |
) | |
} | |
// @Get("/repeat/{value}{/stack") | |
// fun dup(stack: List<Number>): Map<String, Any?> | |
@Get("/{stack:[0-9]+(\\.[0-9]+)?(/[0-9]+(\\.[0-9]+)?)*}/") | |
fun number(stack: String): Map<String, Any?> { | |
val numbers = stack.split("/") | |
// val (_, value) = NumericStack(numbers, 1) | |
val selfHref = "/$stack" | |
return mapOf( | |
"_links" to mapOf( | |
"self" to mapOf( | |
"href" to selfHref, | |
"type" to MediaType.APPLICATION_HAL_JSON | |
) | |
), | |
"stack" to stack | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.helpchoice.kotlin.hal | |
import io.kotlintest.specs.StringSpec | |
internal class StackSampleTest : StringSpec() { | |
init { | |
fun stack() { | |
} | |
fun dup() { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a small sample project I try to make with Micronaut in Kotlin.
Unfortunately the only parameters binding I was able to do as in the code. However I would like to be able to do something like in comments. Is it possible to do that and why it doesn't work for me?
Also for the tests I couldn't figure out how to start the test server from KotlinTest. Any ideas?
Thanx in advance.