Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Jakarta EE: Generation IV — A New Hope

DZone 's Guide to

Jakarta EE: Generation IV — A New Hope

With the brand-new name Jakarta EE — this brings a new hope for Enterprise Java.

· Java Zone ·
Free Resource

Image title

Java Enterprise Edition (Java EE) is an umbrella that holds specifications and APIs with enterprise features, like distributed computing and web services. Widely used in Java, Java EE runs on reference runtimes that can be anything from microservices to application servers that handle transactions, security, scalability, concurrency, and management for the components it’s deploying.

Now that Enterprise Java has been standardized under the Eclipse Foundation — with the brand-new name Jakarta EE — this brings a new hope for Enterprise Java. This post will show a similar solution to Spring MVC with MongoDB on the Jakarta EE community side.

For this project, we’ll need some assistance. To triumph over the imperial forces, these three projects can help us:

  • Eclipse Krazo. An implementation of action-based MVC specified by MVC 1.0 (JSR-371). Eclipse Krazo builds on top of JAX-RS and currently contains support for RESTEasy, Jersey, and CXF with a well-defined SPI for other implementations.

  • Jakarta NoSQL. A framework to help developers create enterprise-grade applications using Java and NoSQL technologies. Jakarta NoSQL enables devs to create scalable applications while maintaining low coupling with the underlying NoSQL technology. 

  •   Bean Validation, JSR 380. A specification that creates its own annotations and validation, and ensures that the properties of a class match specific criteria, using annotations such as @NotNull, @Min, and  @Max.

Show Me the Force  Code

The first step, as usual, is to create a Maven project where we define the dependencies in the pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.superbiz</groupId>
<artifactId>mvc-cxf</artifactId>
<packaging>war</packaging>

<name>OpenEJB :: Examples :: MVC (CXF-based)</name>
<description>OpenEJB :: Web Examples :: MVC 1.0 - Jakarta NoSQL</description>
<version>0.0.1-SNAPSHOT</version>
<url>http://tomee.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <tomee.version>8.0.0-M3</tomee.version>
    <version.krazo>1.0.0-SNAPSHOT</version.krazo>
    <jnosql.version>0.1.0-SNAPSHOT</jnosql.version>
    <http.port>8888</http.port>
</properties>

<build>
    <finalName>ROOT</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.2</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <packagingExcludes>pom.xml</packagingExcludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.tomee.maven</groupId>
            <artifactId>tomee-maven-plugin</artifactId>
            <version>${tomee.version}</version>
            <configuration>
                <tomeeHttpPort>${http.port}</tomeeHttpPort>
            </configuration>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>oss.sonatype.org-snapshot</id>
        <url>http://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>jakarta.enterprise</groupId>
        <artifactId>jakarta.enterprise.cdi-api</artifactId>
        <version>2.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jakarta.ws.rs</groupId>
        <artifactId>jakarta.ws.rs-api</artifactId>
        <version>2.1.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jakarta.validation</groupId>
        <artifactId>jakarta.validation-api</artifactId>
        <version>2.0.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>sh.platform</groupId>
        <artifactId>config</artifactId>
        <version>2.2.2-SNAPSHOT</version>
    </dependency>

    <!--Eclipse JNoSQL-->
    <dependency>
        <groupId>org.jnosql.artemis</groupId>
        <artifactId>artemis-document</artifactId>
        <version>${jnosql.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jnosql.diana</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>${jnosql.version}</version>
    </dependency>

    <!-- MVC 1.0(JSR 371) -->
    <dependency>
        <groupId>org.eclipse.krazo</groupId>
        <artifactId>krazo-core</artifactId>
        <version>${version.krazo}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.krazo</groupId>
        <artifactId>krazo-cxf</artifactId>
        <version>${version.krazo}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

</dependencies>
</project>


One crucial point to remember is that with the Jakarta EE specification, we can use several vendors without impacting the application. In this tutorial, we’ll use Apache TomEE.

The next step is to define the user model. To make this all smoother, this user just has a country, state, name, age, JUG, and description tags as information. So, two classes:

  • The Person class: name, age, JUG, and any description

  • The Address class: country and state

As with any simple register page, the user information comes through in a page form, so the MVC frameworks come with the FormParam annotation. This way, the developer can connect the form with any input field once the annotation matches the input tag exactly.

The MongoDB integration with the model is easy, too; JNoSQL has annotations that look like those of the JPA, so the developer defines the Entity , the Column , and the ID  using the Entity, Column, and Id annotations, respectively.

One difference with MongoDB is that we can store the Address class information as a field instead of making it a relationship, as we usually do with SQL technology. So, we’ll use the Address as a subdocument of the Person entity, which is faster because it reduces the number of joins and is more natural to query.

@Entity
public class Person {

@Id
@FormParam("id")
@Convert(ObjectIdConverter.class)
private String id;

@FormParam("name")
@NotEmpty(message = "can not be empty")
@Size(min = 1, max = 20)
@MvcBinding
@Column
private String name;

@FormParam("age")
@MvcBinding
@Min(18)
@Column
private int age;

@BeanParam
@Valid
@Column
private Address address;

@FormParam("server")
@NotNull
@MvcBinding
@Column
private String server;

@FormParam("description")
@NotEmpty(message = "can not be empty")
@MvcBinding
@Size(min = 10)
@Column
private String description;
}

@Entity
public class Address {

@FormParam("country")
@NotEmpty(message = "can not be empty")
@MvcBinding
@Column
private String country;

@FormParam("state")
@NotEmpty(message = "can not be empty")
@MvcBinding
@Column
private String state;
}


The repository is a DDD pattern that acts as a mediator between the domain and data mapping layers using a collection-like interface for accessing domain objects. To create a Person repository, we need an interface that extends Repository. That’s all; JNoSQL will handle the implementation for you.

Here’s the method with a query whose whole point is to let the Java developer create any method using the JNoSQL convention. Then, the framework will implement it.

public interface PersonRepository extends Repository<Person, String> {

Optional<Person> findById(Long id);

List<Person> findAll();
}


The controller is the bridge to connect the view and the model. Models is a map of the name-to-model instances used by ViewEngine to process a view. Viewable is an abstraction that encapsulates information about a view. The other points are clear enough. Use the GET annotation to define access and POST to define the PATH that establishes a URL path.

Happy Jakarta-ing!

Topics:
jakarta ee ,nosql ,java ee ,java ,jakarta ,enterprise java ,Java ee

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}