URI Variables Example
URI Variables Example
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.PathVariable
@Controller("/issues") (1)
class IssuesController {
@Get("/{number}") (2)
String issue(@PathVariable Integer number) { (3)
"Issue # " + number + "!" (4)
}
}
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
import io.micronaut.context.ApplicationContext;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class IssuesControllerTest {
private static EmbeddedServer server;
private static HttpClient client;
@BeforeClass (1)
public static void setupServer() {
server = ApplicationContext.run(EmbeddedServer.class);
client = server
.getApplicationContext()
.createBean(HttpClient.class, server.getURL());
}
@AfterClass (2)
public static void stopServer() {
if(server != null) {
}
if(client != null) {
client.stop();
}
}
@Test
public void testIssue() throws Exception {
String body = client.toBlocking().retrieve("/issues/12"); (3)
assertNotNull(body);
assertEquals("Issue # 12!", body); (4)
}
@Test
public void testShowWithInvalidInteger() {
HttpClientResponseException e =Assertions.assertThrows(HttpClientResponseException.class, () ->
client.toBlocking().exchange("/issues/hello"));
assertEquals(400, e.getStatus().getCode()); (5)
}
@Test
public void testIssueWithoutNumber() {
HttpClientResponseException e = Assertions.assertThrows(HttpClientResponseException.class, () ->
client.toBlocking().exchange("/issues/"));
assertEquals(404, e.getStatus().getCode()); (6)
}
}
Testing URI Variables
Testing URI Variables
import io.micronaut.context.ApplicationContext
import io.micronaut.http.client.exceptions.HttpClientResponseException
import io.kotlintest.shouldBe
import io.kotlintest.shouldNotBe
import io.kotlintest.shouldThrow
import io.kotlintest.specs.StringSpec
import io.micronaut.http.client.RxHttpClient
class IssuesControllerTest: StringSpec() {
val embeddedServer = autoClose( (2)
ApplicationContext.run(EmbeddedServer::class.java) (1)
val client = autoClose( (2)
embeddedServer.applicationContext.createBean(RxHttpClient::class.java, embeddedServer.getURL()) (1)
)
init {
"test issue" {
val body = client.toBlocking().retrieve("/issues/12") (3)
body shouldNotBe null
body shouldBe "Issue # 12!" (4)
}
"test issue with invalid integer" {
val e = shouldThrow<HttpClientResponseException> { client.toBlocking().exchange<Any>("/issues/hello") }
e.status.code shouldBe 400 (5)
}
"test issue without number" {
val e = shouldThrow<HttpClientResponseException> { client.toBlocking().exchange<Any>("/issues/") }
e.status.code shouldBe 404 (6)
}
}
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
.