This project uses Quarkus, the Supersonic Subatomic Java Framework. This project has been used to present Quarkus in Codemotion Madrid 2019. The slides are available [here](Codemotion 2019 Descubriendo Quarkus%2C java sub-atómico en acción.pdf).
This project is the result of following the Quarkus getting started guide.
If you want to learn more about Quarkus, please visit its website: https://quarkus.io/ .
- Generate project
mvn io.quarkus:quarkus-maven-plugin:1.5.2.Final:create \
-DprojectGroupId=org.acme \
-DprojectArtifactId=getting-started \
-DclassName="org.acme.getting.started.GreetingResource" \
-Dpath="/hello"
- Navigate to the directory and launch the application
cd getting-started mvn compile quarkus:dev
- Open browser to http://localhost:8080
- Open browser to http://localhost:8080/hello
- Change the greeting message in the
HelloResource
, refresh browser
- Add method:
@GET @Produces(MediaType.TEXT_PLAIN) @Path("/city") public String city() { return "Madrid"; }
- Open browser to http://localhost:8080/hello/city
- In the resource, add
@ConfigProperty(name = "greeting") String greeting;
- Change the hello method to return the greeting message:
@GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return greeting; }
- Open browser to http://localhost:8080/hello
- We get an error because we have not added the property
greeting
to the configuration file - Open the
application.properties
file and add:greeting=Hola
- Refresh browser
- In the resource class, add an
Optional<String>
:@ConfigProperty(name = "city") Optional<String> city;
- Replace the
city
method with:@GET @Produces(MediaType.TEXT_PLAIN) @Path("/city") public String city() { return city.orElse("Barcelona"); }
- Open browser to http://localhost:8080/hello/city
- Create class
MyBean
in theorg.acme.quickstart
package with the following content:@ApplicationScoped public class MyBean { @ConfigProperty(name = "greeting") String greeting; public String greeting() { return greeting; } }
- Update the content of
HelloResource
to become:@Inject MyBean bean; @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return bean.greeting(); }
- Open browser to http://localhost:8080/hello
The application can be packaged using ./mvnw package
.
It produces the getting-started-1.0-SNAPSHOT-runner.jar
file in the /target
directory.
The application is now runnable using java -jar target/getting-started-1.0-SNAPSHOT-runner.jar
.
You can create a native executable using: ./mvnw package -Pnative
.
You can then execute your native executable with: ./target/getting-started-1.0-SNAPSHOT-runner
Or, if you don't have GraalVM installed, you can run the native executable build in a container using: ./mvnw package -Pnative -Dquarkus.native.container-build=true
.
Then, build the docker image with docker build -f src/main/docker/Dockerfile.native -t quarkus/getting-started .
Finally, run the container using docker run -i --rm -p 8080:8080 quarkus/getting-started
If you want to learn more about building native executables, please consult https://quarkus.io/guides/building-native-image.