Hello Quarkus Supersonic Subatomic Java World

Hello Quarkus Supersonic Subatomic Java World


Quarkus is the new Supersonic Subatomic Java framework tailored for GraalVM and HotSpot.
In this quick tutorial, I will show you how to get started with Quarkus.


Create a Quarkus Project


You can create a Quarkus project by using the Maven command or by going too code.quarkus.io, I will create the Quarkus project using the Maven plugin.
Open the terminal and cd to an appropriate parent directory for your project and use the following command to create the application.


$ mvn io.quarkus:quarkus-maven-plugin:1.0.0.CR1:create \
    -DprojectGroupId=com.kodnito \
    -DprojectArtifactId=hello-quarkus-world \
    -DclassName="com.kodnito.quickstart.HelloResource" \
    -Dpath="/hello-quarkus"


Navigate into the project directory and run the project:


$ ./mvnw compile quarkus:dev


quarkus:dev runs Quarkus in development mode, which means that you don't have to quit and restart Quarkus and if there are issues an error page will let you know. When the application is started, you can request the provided endpoint by using the following curl command:


$ curl http://localhost:8080/hello-quarkus
hello%


Don't stop Quarkus and edit the src/main/java/com/kodnito/quickstart/HelloResource.java file to match the following:


package com.kodnito.quickstart;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello-quarkus")
public class HelloResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "hello Quarkus";
    }
}


Now test again with the curl command and you should get the following output:


$ curl -w "\n" http://localhost:8080/hello-quarkus
hello Quarkus%


curl -w "\n" will not print the %

Testing


There is 2 test dependencies in the pom.xml file and are automatically generated when you generate the project.
Stop Quarkus and run the following command to run the tests:


$ ./mvnw test

[ERROR] Failures:
[ERROR]   HelloResourceTest.testHelloEndpoint:18 1 expectation failed.
Response body doesn't match expectation.
Expected: is "hello"
  Actual: hello Quarkus

[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0


The test should fail because we changed the output when you request /hello-quarkus endpoint.
To make the test pass edit the src/test/java/com/kodnito/quickstart/HelloResourceTest.java to match the following:


package com.kodnito.quickstart;

import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;

@QuarkusTest
public class HelloResourceTest {

    @Test
    public void testHelloEndpoint() {
        given()
          .when().get("/hello-quarkus")
          .then()
             .statusCode(200)
             .body(is("hello Quarkus"));
    }

}



$ ./mvnw test

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.kodnito.quickstart.HelloResourceTest
2019-11-16 22:27:41,071 INFO  [io.qua.dep.QuarkusAugmentor] (main) Beginning quarkus augmentation
2019-11-16 22:27:42,448 INFO  [io.qua.dep.QuarkusAugmentor] (main) Quarkus augmentation completed in 1377ms
2019-11-16 22:27:43,154 INFO  [io.quarkus] (main) Quarkus 1.0.0.CR1 started in 0.668s. Listening on: http://0.0.0.0:8081
2019-11-16 22:27:43,155 INFO  [io.quarkus] (main) Profile test activated.
2019-11-16 22:27:43,156 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.769 s - in com.kodnito.quickstart.HelloResourceTest
2019-11-16 22:27:44,729 INFO  [io.quarkus] (main) Quarkus stopped in 0.058s
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.504 s
[INFO] Finished at: 2019-11-16T22:27:45+01:00
[INFO] ------------------------------------------------------------------------


Now our test passes.

Spring Developers


With Quarkus you can define RESTful services with Spring Web annotations.
You have to add the spring-web extension to your pom.xml file.
The easiest way to add extensions is to use the maven plugin.
Type the following command to list all Quarkus extensions:


$ ./mvnw quarkus:list-extensions


You use the following command to install the extension(s) you want to use:


$ ./mvnw quarkus:add-extension -Dextensions="quarkus-spring-web"

Here I'm installing the spring-web extension.
Create the src/main/java/kodnito/quickstart/HelloSpringResource.java file with the following content:


package com.kodnito.quickstart;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello-spring")
public class HelloSpringResource {

    @GetMapping()
    public String helloSpring() {
        return "Hello Spring";
    }
}


Now when you request the endpoint you should see the following output:


$ curl -w "\n" http://localhost:8080/hello-spring
Hello Spring


Packaging the application:
Run the following command to package the application:


./mvnw package


This command will create 2 jar files and you can run the application using:


$ java -jar target/hello-quarkus-world-1.0-SNAPSHOT-runner.jar



GraalVM


If you have GraalVM installed on your machine, you can create a native executable using:


$ ./mvnw verify -Pnative


This will take few minutes and when it's done you should have a executable file in the target folder.


$ ./target/hello-quarkus-world-1.0-SNAPSHOT-runner
2019-11-16 22:59:06,289 INFO  [io.quarkus] (main) hello-quarkus-world 1.0-SNAPSHOT (running on Quarkus 1.0.0.CR1) started in 0.014s. Listening on: http://0.0.0.0:8080
2019-11-16 22:59:06,289 INFO  [io.quarkus] (main) Profile prod activated.
2019-11-16 22:59:06,289 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy, resteasy-jackson, spring-di, spring-web]
2019-11-16 22:59:11,216 INFO  [io.quarkus] (main) hello-quarkus-world stopped in 0.005s


Wow, it starts in 0.005s, now that is really cool.
In this tutorial, I showed you how to get started with Quarkus and more tutorials about Quarkus will come.


Share this: