This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Quarkus 3.4 – Container-first Java Stack: Install with OpenJDK 21 and Create REST API




Abstract

Quarkus is certainly one of Java frameworks for microservices improvement and cloud-native deployment. It’s developed as container-first stack and dealing with GraalVM and HotSpot digital machines (VM).

Java was not prepared for the cloud-computing period as soon as. It is as a result of Java and its ecosystem had been developed primarily for providers which had been anticipated to be all the time energetic or at the very least at most time. As soon as their servers and providers had been began, they saved reminiscence consumed after some initiation time. Their providers had been anticipated to not be stopped or destroyed as soon as began.

In distinction, providers within the cloud remarkably rely on virtualization. VMs are often created and destroyed in cloud surroundings. The weak factors Java revealed then had been as under:

  • Gradual boot time
  • Excessive reminiscence consumption

As to cloud improvement and deployment, pushed by container VM and Kubernetes pods and so forth., required quick begin as much as machines.

After many contributions by many, progress and new encounters with GraalVM and so forth., Java lit the best way to unravel the issue finally.

This put up reveals set up Quarkus with OpenJDK 21 on Devuan 5 and create an instance venture to serve JSON REST APIs.



Surroundings

  • OS: Devuan 5 Daedalus
  • App Engine: OpenJDK 21
  • Mission Construct and Administration: Apache Maven 3 (3.9.5), Gradle 8 (8.3)
  • Java stack: Quarkus 3 (3.4.3)



Tutorial

Suppose you’ve gotten OpenJDK 21 and Maven that are prepared with surroundings variables PATH and JAVA_HOME set.

With Devuan used, my previous posts to put in Java 21 and Maven will assist.



Start line šŸ’«

My surroundings was as under:

$ java --version
openjdk 21 2023-09-19
OpenJDK Runtime Surroundings (construct 21+35-2513)
OpenJDK 64-Bit Server VM (construct 21+35-2513, combined mode, sharing)

$ mvn --version
Apache Maven 3.9.5 (57804ffe001d7215b5e7bcb531cf83df38f93546)
Maven house: /(...)/apache-maven-3.9.5
Java model: 21, vendor: Oracle Company, runtime: /(...)/jdk-21
Default locale: en_US, platform encoding: UTF-8
OS title: "linux", model: "6.1.0-13-amd64", arch: "amd64", household: "unix"
Enter fullscreen mode

Exit fullscreen mode



Create a Quarkus venture šŸ¦Æ

Maven helps to create a venture as under:

$ mvn io.quarkus.platform:quarkus-maven-plugin:3.4.3:create 
    -Dextensions="resteasy-reactive-jackson" 
    -DnoCode 
    -DprojectGroupId=com.quarkusrestjsonexample 
    -DprojectArtifactId=quarkus-rest-json-example 
    -DbuildTool=gradle
Enter fullscreen mode

Exit fullscreen mode

The final -DbuildTool=gradle is elective. I want Gradle construct.

The consequence was as under:

[INFO] Scanning for initiatives...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Constructing Maven Stub Mission (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- quarkus:3.4.3:create (default-cli) @ standalone-pom ---
[INFO] -----------
[INFO] chosen extensions: 
- io.quarkus:quarkus-resteasy-reactive-jackson

[INFO] 
making use of codestarts...
[INFO] šŸ“š java
šŸ”Ø gradle
šŸ“¦ quarkus
šŸ“ config-properties
šŸ”§ dockerfiles
šŸ”§ gradle-wrapper
[INFO] 
-----------
[SUCCESS] āœ…  quarkus venture has been efficiently generated in:
--> /(...)/quarkus-rest-json-example
-----------
[INFO] 
[INFO] ========================================================================================
[INFO] Your new software has been created in /(...)/quarkus-rest-json-example
[INFO] Navigate into this listing and launch your software with mvn quarkus:dev
[INFO] Your software might be accessible on http://localhost:8080
[INFO] ========================================================================================
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Complete time:  1.209 s
[INFO] Completed at: 2023-10-15T20:38:05+09:00
[INFO] ------------------------------------------------------------------------
Enter fullscreen mode

Exit fullscreen mode



Test construct dependencies šŸ”

Let’s go contained in the venture:

$ cd rest-json-quickstart
Enter fullscreen mode

Exit fullscreen mode

You’ll be able to see implementation 'io.quarkus:quarkus-resteasy-reactive-jackson' in dependencies:

$ grep -a5 'dependencies {' construct.gradle
repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
    implementation 'io.quarkus:quarkus-resteasy-reactive-jackson'
    implementation 'io.quarkus:quarkus-arc'
    testImplementation 'io.quarkus:quarkus-junit5'
}
Enter fullscreen mode

Exit fullscreen mode

OK.



Create .java recordsdata in src āœ

There are not any .java recordsdata in src by default:

$ ls src/predominant/java/
Enter fullscreen mode

Exit fullscreen mode

Right here we are going to create two recordsdata in src/predominant/java: Fruit.java and FruitResource.java as bean and useful resource.

$ nvim src/predominant/java/{Fruit.java,FruitResource.java}
Enter fullscreen mode

Exit fullscreen mode

Write as under in Fruit.java:

bundle com.quarkusrestjsonexample;

public class Fruit {

    public String title;
    public String description;

    public Fruit() {
    }

    public Fruit(String title, String description) {
        this.title = title;
        this.description = description;
    }
}
Enter fullscreen mode

Exit fullscreen mode

Once you use NeoVim/Vim, it’s best to kind :subsequent after :w (save) with a view to change recordsdata.

Then write as under in FruitResource.java:

bundle com.quarkusrestjsonexample;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Set;

import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;

@Path("/fruits")
public class FruitResource {

    non-public Set<Fruit> fruits = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));

    public FruitResource() {
        fruits.add(new Fruit("Apple", "Winter fruit"));
        fruits.add(new Fruit("Pineapple", "Tropical fruit"));
    }

    @GET
    public Set<Fruit> checklist() {
        return fruits;
    }

    @POST
    public Set<Fruit> add(Fruit fruit) {
        fruits.add(fruit);
        return fruits;
    }

    @DELETE
    public Set<Fruit> delete(Fruit fruit) {
        fruits.removeIf(existingFruit -> existingFruit.title.contentEquals(fruit.title));
        return fruits;
    }
}
Enter fullscreen mode

Exit fullscreen mode

Sort :wq to shut NeoVim/Vim.



Run service šŸš€

Gradle helps to construct it and begin service as under:

$ ./gradlew --console=plain quarkusDev
Enter fullscreen mode

Exit fullscreen mode

Then your server will begin:

Beginning a Gradle Daemon (subsequent builds might be quicker)
(...)
__  ____  __  _____   ___  __ ____  ______ 
 --/ __ / / / / _ | / _ / //_/ / / / __/ 
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /    
--________/_/ |_/_/|_/_/|_|____/___/   
2023-10-15 20:49:36,188 INFO  [io.quarkus] (Quarkus Fundamental Thread) quarkus-rest-json-example 1.0.0-SNAPSHOT on JVM (powered by Quarkus 3.4.3) began in 1.242s. Listening on: http://localhost:8080
2023-10-15 20:49:36,190 INFO  [io.quarkus] (Quarkus Fundamental Thread) Profile dev activated. Dwell Coding activated.
2023-10-15 20:49:36,191 INFO  [io.quarkus] (Quarkus Fundamental Thread) Put in options: [cdi, resteasy-reactive, resteasy-reactive-jackson, smallrye-context-propagation, vertx]
Enter fullscreen mode

Exit fullscreen mode

Yay šŸ™Œ



Conclusion

In keeping with the Gradle output above, entry to http://localhost:8080 along with your internet browser.
You will note:

quarkus-01

Why 404 ? It is as a result of there isn’t a definition about @Path("https://style-tricks.com/"), the basis path, in FruitResource.java. It is OK now, for it may be prolonged simply.

Then go to /fruits and you will notice šŸ˜‰

quarkus-02

Our Quarkus Net API service is operating in good well being.



Acknowledgements

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?