Recently I tried Quarkus for a fast development and was amazed by how fast this tool enable my project to lift off the ground. Here are the simple step to get started.
mvn io.quarkus:quarkus-maven-plugin:1.9.2.Final:create \
-DprojectGroupId=org.acme \
-DprojectArtifactId=quarkus-bootstrap \
-DclassName="org.acme.getting.started.GreetingResource" \
-Dpath="/hello"
quarkus-bootstrap
Create an application service class
package org.acme.getting.started;
import javax.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class GreetingService {
public String greeting(String name) {
return "Hello " + name;
}
}
Inject to quarkus REST resource class
package org.acme.getting.started;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class GreetingResource {
@Inject
GreetingService greetingService;
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/greeting/{name}")
public String hello(@PathParam("name") String name) {
return greetingService.greeting(name);
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
}
It’s even super fast for writing test, natually!
package org.acme.getting.started;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
@QuarkusTest
public class GreetingResourceTest {
@Test
public void testHelloEndpoint() {
given()
.when().get("/hello")
.then()
.statusCode(200)
.body(is("hello"));
}
@Test
public void testGreetingEndpoint() {
String uuid = UUID.randomUUID().toString();
given()
.pathParam("name", uuid)
.when().get("/hello/greeting/{name}")
.then()
.statusCode(200)
.body(is("Hello " + uuid));
}
}
Now let’s run the test, and let’s test the endpoint as well
# Run tests
mvn test
T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.acme.getting.started.GreetingResourceTest
2020-11-16 21:50:34,668 INFO [io.quarkus] (main) Quarkus 1.9.2.Final on JVM started in 1.172s. Listening on: http://0.0.0.0:8081
2020-11-16 21:50:34,686 INFO [io.quarkus] (main) Profile test activated.
2020-11-16 21:50:34,686 INFO [io.quarkus] (main) Installed features: [cdi, resteasy]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.342 s - in org.acme.getting.started.GreetingResourceTest
2020-11-16 21:50:35,678 INFO [io.quarkus] (main) Quarkus stopped in 0.030s
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
# Run dev mode with hot deployment
mvn quarkus:dev
❯ curl -w "\n" http://localhost:8080/hello
hello
❯ curl -w "\n" http://localhost:8080/hello/greeting/quarkus
Hello quarkus
❯ curl -w "\n" http://localhost:8080/hello/greeting/quarkus
Hello quarkus
# Let's pack a jar file and test as well
mvn clean package
java -jar target/getting-started-1.0-SNAPSHOT-runner.jar
curl -w "\n" http://localhost:8080/hello/greeting/quarkus
Hello quarkus
Add to my src(0) Please login to bookmark
No account yet? Register