URI Variables Example

    URI Variables Example

    1. import io.micronaut.http.annotation.Get
    2. import io.micronaut.http.annotation.PathVariable
    3. @Controller("/issues") (1)
    4. class IssuesController {
    5. @Get("/{number}") (2)
    6. String issue(@PathVariable Integer number) { (3)
    7. "Issue # " + number + "!" (4)
    8. }
    9. }

    URI Variables Example

    Micronaut will map the URI /issues/{number} for the above controller. We can assert this is the case by writing a set of unit tests:

    Testing URI Variables

    1. import io.micronaut.context.ApplicationContext;
    2. import io.micronaut.http.client.HttpClient;
    3. import io.micronaut.http.client.exceptions.HttpClientResponseException;
    4. import io.micronaut.runtime.server.EmbeddedServer;
    5. import org.junit.AfterClass;
    6. import org.junit.BeforeClass;
    7. import org.junit.Test;
    8. import org.junit.jupiter.api.Assertions;
    9. import static org.junit.Assert.assertEquals;
    10. import static org.junit.Assert.assertNotNull;
    11. public class IssuesControllerTest {
    12. private static EmbeddedServer server;
    13. private static HttpClient client;
    14. @BeforeClass (1)
    15. public static void setupServer() {
    16. server = ApplicationContext.run(EmbeddedServer.class);
    17. client = server
    18. .getApplicationContext()
    19. .createBean(HttpClient.class, server.getURL());
    20. }
    21. @AfterClass (2)
    22. public static void stopServer() {
    23. if(server != null) {
    24. }
    25. if(client != null) {
    26. client.stop();
    27. }
    28. }
    29. @Test
    30. public void testIssue() throws Exception {
    31. String body = client.toBlocking().retrieve("/issues/12"); (3)
    32. assertNotNull(body);
    33. assertEquals("Issue # 12!", body); (4)
    34. }
    35. @Test
    36. public void testShowWithInvalidInteger() {
    37. HttpClientResponseException e =Assertions.assertThrows(HttpClientResponseException.class, () ->
    38. client.toBlocking().exchange("/issues/hello"));
    39. assertEquals(400, e.getStatus().getCode()); (5)
    40. }
    41. @Test
    42. public void testIssueWithoutNumber() {
    43. HttpClientResponseException e = Assertions.assertThrows(HttpClientResponseException.class, () ->
    44. client.toBlocking().exchange("/issues/"));
    45. assertEquals(404, e.getStatus().getCode()); (6)
    46. }
    47. }

    Testing URI Variables

    Testing URI Variables

    1. import io.micronaut.context.ApplicationContext
    2. import io.micronaut.http.client.exceptions.HttpClientResponseException
    3. import io.kotlintest.shouldBe
    4. import io.kotlintest.shouldNotBe
    5. import io.kotlintest.shouldThrow
    6. import io.kotlintest.specs.StringSpec
    7. import io.micronaut.http.client.RxHttpClient
    8. class IssuesControllerTest: StringSpec() {
    9. val embeddedServer = autoClose( (2)
    10. ApplicationContext.run(EmbeddedServer::class.java) (1)
    11. val client = autoClose( (2)
    12. embeddedServer.applicationContext.createBean(RxHttpClient::class.java, embeddedServer.getURL()) (1)
    13. )
    14. init {
    15. "test issue" {
    16. val body = client.toBlocking().retrieve("/issues/12") (3)
    17. body shouldNotBe null
    18. body shouldBe "Issue # 12!" (4)
    19. }
    20. "test issue with invalid integer" {
    21. val e = shouldThrow<HttpClientResponseException> { client.toBlocking().exchange<Any>("/issues/hello") }
    22. e.status.code shouldBe 400 (5)
    23. }
    24. "test issue without number" {
    25. val e = shouldThrow<HttpClientResponseException> { client.toBlocking().exchange<Any>("/issues/") }
    26. e.status.code shouldBe 404 (6)
    27. }
    28. }

    Note that the URI template in the previous example requires that the number variable is specified. You can specify optional URI templates with the syntax: /issues{/number} and by annotating the number parameter with @Nullable.