JavaRush /Java Blog /Random EN /Coffee break #119. Introduction to Java Enterprise Editio...

Coffee break #119. Introduction to Java Enterprise Edition (Java EE)

Published in the Random EN group
Source: Java Enterprise Edition (Java EE) is a set of specifications and documentation for the Java language that describes a server platform architecture for solving the problems of medium and large enterprises. Coffee break #118.  Introduction to Java Enterprise Edition (Java EE) - 1When learning Java EE you will come across the following terms:
  • An application server is a concrete implementation of the abstract Java EE specifications. Examples: Payara Server (Glassfish), IBM OpenLiberty and JBoss Wildfly.

  • A Java Specification Request (JSR) is a formal request to the Java community to add and improve technologies. It is a body that standardizes APIs on the Java technology platform and is used to group APIs into blocks, such as JAX-RS (Java API for RESTful Web Services). For every JSR, there is always a default reference implementation.

  • A reference implementation is a concrete implementation/implementation of an abstract JSR. For example, the reference implementation for JAX-RS is called Jersey. Java EE itself is a JSR. Thus, the application server is a collection of different Java EE JSR reference implementations. Java EE is JSR 366, and one of its reference implementations is Glassfish 5.

  • Jakarta EE is the new name for Java EE starting in 2018. Currently, Oracle has transferred the Java platform under the management of the Eclipse Foundation .

JavaEE Basics

There are three key APIs in Java EE:
  1. Java Persistence API (JPA). It is responsible for storing and retrieving information from relational databases, which can be extended to work with NoSQL databases. This is the data layer in the application.

  2. Context and Dependency Injection (CDI API). It is a standardized way to reduce coupling in applications. It manages various component interactions to ensure free decoupling.

  3. Java API for RESTful Web Services (JAX-RS). It provides resources over HTTP as web services.

Context and Dependency Injection API

Dependency injection is a special form of inversion control (a software strategy in which individual components receive their dependencies). This externalizes the dependency in the application to create low coupling components.

Features of CDI

  • Dependency injection (Typesafe) allows you to declare dependencies on types so that the compiler catches errors at runtime.

  • Lifecycle contexts are the ability to tie lifecycle and stateful component interactions to well-defined but extensible lifecycle components.

  • Interceptors allow you to intercept requests to access a specific method.

  • Events are a way to develop highly decoupled applications. Events can be fired while the Observers method is listening to the fired events.

  • Service Provider Interface (SPI) is a set of techniques, APIs, and interfaces that can be used as extensions, such as in the Apache library.

Some CDI API concepts:

  • CDI Bean Discovery is a mechanism in which the dependency injection framework parses and discovers beans to control how beans are discovered by default, that is, beans with annotations such as @Annotated. There are three types of bean discovery mode: ALL (including unannotated beans), ANNOTATED, and NONE.

  • A CDI container is a factory method where Java classes come in and out with their functionality and capabilities. This is the application that manages the beans.

  • Bean and Contextual instances. A bean is a template that a developer creates. A contextual instance is an instance of a Bean created and managed by a CDI container.

Java Persistence API

JPA is used to map objects to relational database tables. The Java Persistence API follows the principles of the ORM manifesto. ORM Manifest (Object Relational Mapping Manifest):
  • Objects, not tables: Developers write objects, not tables.

  • Convenience, not ignorance: An ORM should be convenient. Developers should have a minimum knowledge of relational databases. ORM is not a way to hide ignorance, but a convenience.

  • Unobtrusive and transparent: An ORM should make it so that developers can control what is in the database and have full control over what is saved. Legacy data, new objects: ORM will allow you to create new objects from legacy data, that is, reconstruct the legacy database into Java objects.

  • Enough, but not too much: ORM will provide all the tools to solve common problems due to impedance mismatch (a term used to refer to problems that arise due to differences between the database model and the programming language). An ORM should not be overly heavy.

  • Locality and mobility: Data is local, but it must be possible for the application's persistent state to move to different parts of the application.

  • Standard API, pluggable implementation: Rely on the standard API, but can change implementations as needed.

Some important JPA concepts: JPA Entity
  • The most modular component of JPA objects is the plain old Java object (POJO). Each object must have a unique identifier.

  • JPA entities typically use annotations such as @MappedSuperClass, which allows the use of superclasses containing common entity fields. The @AttributeOverride annotation is used to override superclass entities. @Column is used to configure database mappings. The @Transient annotation can be used for fields in an entity class that should not be mapped to a database.

  • Access type is the process by which the persistence provider accesses states in an entity. Field access occurs when the provider accesses the fields of a class directly through reflection. Property access occurs when Java Bean property methods are used to access states, that is, getters and setters are used. To use property access, the getter must be annotated with @Id. Mixed access type uses access to both fields and properties in the same entity class using the @Access annotation.

Java API for RESTful web services

Limitations of the REST architecture:
  1. The client and server are independent of each other.

  2. Stateless: Each individual request that arrives at the server is autonomous and unique. The server does not make any assumptions from the previous request.

  3. Cacheability: The system must support caching at different levels to improve performance and scalability.

  4. A unified interface means that the client must have a common unified interface to access resources on the server, as well as to interact with resources on the server. Multi-tier system: The server can be implemented in different layers in such a way that the client does not have to worry about a multi-tier system, such as a server that supports load balancing.

Some general concepts related to JAX-RS:
  • HTTP GET method: request for a resource or resources. The GET request method is idempotent, which means that making the same request to the method repeatedly should not change the state of the resource or data on the server.

  • HTTP POST method: The POST request is used to create new resources on the server. This causes the data set on the server to change. POST usually has a body in which the payload or whatever we want to create on the server is published or attached.

  • HTTP PUT method: Used semantically to update resources on the server.

  • HTTP DELETE Method: Used to delete resources on the server.

  • Content types. There are several types of content that can be consumed and created by query methods: XML, CSV, EXCEL, TEXT, and JSON.

  • JAX-RS has the concept of writing a message body. These are API constructs used to convert Java types to the type expected by the client. The @Produces annotation can be used to specify the type to which the response of a Java object is converted.

  • The @Consumes annotation tells the JAX-RS runtime the type of content that a given resource method is consuming. The JAX-RS runtime then converts the JSON content passed as the payload into a Java object whose type is the same as the method parameter.

  • JAX-RS Exception Mapper: An API construct used to map exceptions to HTTP responses. The @Provider annotation is used to programmatically register an exception resolver with the JAX-RS runtime.

This is a very simple introduction to Java EE. To build web applications using Java EE, you'll need to dive deeper into each key API.