You are a programming tutorial writer. Write very long tutorial on hello world in Quarkus.
- User
You are a programming tutorial writer. Write very long tutorial on hello world in Quarkus.
- Assistant
You are a programming tutorial writer. Write very long tutorial on hello world in Quarkus.
Quarkus is a full-stack, Kubernetes-native Java framework made for Java virtual machines (JVMs) and native compilation, optimizing Java specifically for containers and enabling it to become an effective platform for serverless, cloud, and Kubernetes environments.
Before you begin, ensure you have the following installed:
While you can add Quarkus to an existing Maven project, the best way to use Quarkus is by bootstrapping a project with the Quarkus Maven plugin:
mvn io.quarkus:quarkus-maven-plugin:1.13.7.Final:create \
-DprojectGroupId=org.acme \
-DprojectArtifactId=hello-quarkus \
-DclassName="org.acme.quickstart.GreetingResource" \
-Dpath="/hello"
Replace 1.13.7.Final with the version of Quarkus you want to use.
Upon executing the previous Maven command, a new Quarkus project is generated. Here is a breakdown of the generated project structure:
src/main/java: Contains your Java codesrc/main/resources: Contains resources such as properties filessrc/test/java: Contains your test codepom.xml: The Maven build descriptorQuarkus generates a simple REST endpoint for you located at src/main/java/org/acme/quickstart/GreetingResource.java:
package org.acme.quickstart;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class GreetingResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello World";
}
}
This simple resource exposes a hello method on the path /hello which will return a plain text "Hello World".
To run your Quarkus application in dev mode, which enables live coding, use the following Maven command:
./mvnw compile quarkus:dev
Once the server has started, visit http://localhost:8080/hello in your browser. You should see the "Hello World" message displayed.
Let's expand upon the "Hello World" by adding a simple JSON endpoint.
First, let's add the JSON-B support extension:
./mvnw quarkus:add-extension -Dextensions="resteasy-jsonb"
Create a new Java class in src/main/java/org/acme/quickstart/HelloResource.java:
package org.acme.quickstart;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/json")
public class HelloResource {
public static class Greeting {
public String message;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Greeting json() {
Greeting greeting = new Greeting();
greeting.message = "Hello, World!";
return greeting;
}
}
This new resource provides an object representation of a greeting message. When you call /json, it will respond back with a JSON object structured like {"message":"Hello, World!"}.
You do not need to restart the server when making changes like this. Quarkus's live coding feature should have picked up the changes automatically.
Visit http://localhost:8080/json on your browser or use a tool like curl:
curl http://localhost:8080/json
You should see the JSON response:
{"message":"Hello, World!"}
To compile your application into a native executable, use the following Maven profile:
./mvnw package -Pnative
If you do not have GraalVM installed, you can use a Docker container to build your native executable like so:
./mvnw package -Pnative -Dquarkus.native.container-build=true
After the process completes, you will find a native executable in the target directory.
Run the native executable by accessing the file in the target directory:
./target/hello-quarkus-1.0.0-SNAPSHOT-runner
Your native Quarkus app will start almost instantaneously.
Congratulations! You have now learned how to create, expand, and build a native "Hello World" application using Quarkus. This tutorial covered setting up Quarkus, creating a simple REST application, adding JSON support, and compiling to a native executable. You can explore further by adding database support, implementing security, and packaging your app for deployment on Kubernetes. Quarkus provides a robust foundation for building modern, efficient Java applications.
Happy coding with Quarkus!