Backend of Comparabien.com.pe with JakartaEE, DeltaSpike and Blaze Persistence







Prerequisites


In this article we are going to remember 2 previous articles that are:


Source Code


The backend source code is in this link https://github.com/joedayz/ConcentradorJakartaEE.
The frontend source code is in this link https://github.com/joedayz/concentrador.

The intention is to show the great work of my colleague Jorge Pingo.

Jakarta EE and MicroProfile


In this article we will not use Spring Boot, we will use JakartaEE and Microprofile to be able to use the APIs that allow us to create Rest services with JAX-RS and persistence with JPA and CDI.



Apis from Jakarta EE:



Apis from Microprofile:



Persistence.xml

In this file, we configure the JDBC resource that we will have in Payara Server.


In Payara Server the JDBC Connection Pool will be defined:


And JDBC Resources:



Blaze Persistence

For the Backend, we are going to use this API that allows you to write JPA Criteria queries that use advanced SQL features. The source code for the project is on GitHub and there you can find various use cases.



DeltaSpike


Another project to use in this project is DeltaSpike which consists of a number of portable CDI extensions that provide useful features for Java application developers.
It requires a CDI implementation that is available in runtime.
These features are in addition to those provided by the CDI specification. In this link you can find the complete list:



In the file apache-deltaspike.properties we will configure:

globalAlternatives.org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy = org.apache.deltaspike.jpa.impl.transaction.BeanManagedUserTransactionStrategy



NOTE:

By default the transaction type used by @Transactional is RESOURCE_LOCAL. If you set the type of transaction-type = "JTA" in the persistence.xml, you have to enable another TransactionStrategy in the beans.xml which is called org.apache.deltaspike.jpa.impl.transaction.BeanManagedUserTransactionStrategy.






Context Root


The context root is defined in the glassfish-web.xml file like this:


Modelo

In the model package, you will find the JPA entities and the entities related to views.



Repository

Thanks to DeltaSpike we can have a Repository that will implement for us the methods to persist our entity and we can also create our own Object Query.






This is very similar to Spring Data.

Service

The business layer will be able to use the repository layer only using @Inject.



In this layer, we will find the ConsultaProductoServiceImpl.java class that the Criteria API will use to build the query according to the filters we have in the application. In addition to that, the implementation of paging.

To achieve this DeltaSpike is used:

import com.blazebit.persistence.deltaspike.data.Page;
import com.blazebit.persistence.deltaspike.data.PageRequest;
import com.blazebit.persistence.deltaspike.data.Pageable;
import com.blazebit.persistence.deltaspike.data.Sort;
import com.blazebit.persistence.deltaspike.data.Specification;

Controller

Finally, in this layer, we will have the REST endpoints. We start by configuring the class that inherits from the Application.


And then you can create your controller classes to handle the endpoints you need:



As you can see, you can define the scope, the main path and the exchange format (JSON) between server and clients:

@RequestScoped
@Path("/entidad-financiera")
@Produces("application/json")
@Consumes("application/json")
public class EntidadFinancieraController 

Running the Backend

To test the application, first, make sure to start your payara server.


Then configure the payara server in your IDE to be able to deploy the application there:





When you run the application you will see the following result:


And you can try this endpoint (http://localhost:8080/api/v1/concentrador/opcion) to see the available options:





Configuring the Backend in the FrontEnd

In order to use the backend, we put the base endpoint in the environment.




BASE_ENDPOINT: 'http://localhost:8080/api/v1/concentrador'

When executing the application we will obtain the following result:



And as you can see, you can see the available options that the backend returns to us.

We hope you enjoy it.

Enjoy!

Joe


Share:

0 comentarios:

Publicar un comentario