Java EE 8 Cookbook
eBook - ePub

Java EE 8 Cookbook

Elder Moraes

Buch teilen
  1. 382 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Java EE 8 Cookbook

Elder Moraes

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

A practical guide for building effective enterprise solutions with Java EE 8

Key Features

  • Recipes to get you up-and-running with Java EE 8 application development
  • Learn how to apply the major Java EE 8 APIs and specifications
  • Implement microservices and Reactive programming with Java EE 8

Book Description

Java EE is a collection of technologies and APIs to support Enterprise Application development. The choice of what to use and when can be dauntingly complex for any developer. This book will help you master this. Packed with easy to follow recipes, this is your guide to becoming productive with Java EE 8.

You will begin by seeing the latest features of Java EE 8, including major Java EE 8 APIs and specifications such as JSF 2.3, and CDI 2.0, and what they mean for you.

You will use the new features of Java EE 8 to implement web-based services for your client applications. You will then learn to process the Model and Streaming APIs using JSON-P and JSON-B and will learn to use the Java Lambdas support offered in JSON-P. There are more recipes to fine-tune your RESTful development, and you will learn about the Reactive enhancements offered by the JAX-RS 2.1 specification.

Later on, you will learn about the role of multithreading in your enterprise applications and how to integrate them for transaction handling. This is followed by implementing microservices with Java EE and the advancements made by Java EE for cloud computing.

The final set of recipes shows you how take advantage of the latest security features and authenticate your enterprise application.

At the end of the book, the Appendix shows you how knowledge sharing can change your career and your life.

What you will learn

  • Actionable information on the new features of Java EE 8
  • Using the most important APIs with real and working code
  • Building server side applications, web services, and web applications
  • Deploying and managing your application using the most important Java EE servers
  • Building and deploying microservices using Java EE 8
  • Building Reactive application by joining Java EE APIs and core Java features
  • Moving your application to the cloud using containers
  • Practical ways to improve your projects and career through community involvement

Who this book is for

This book is for developers who want to become proficient with Java EE 8 for their enterprise application development. Basic knowledge of Java is assumed

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Java EE 8 Cookbook als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Java EE 8 Cookbook von Elder Moraes im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Computer Science & Programming in Java. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
2018
ISBN
9781788290258

Server-Side Development

Java EE can be seen as being made for server-side development. Most of the APIs are powerful for server-side processing and managing.
This chapter will provide you with some common and useful scenarios that you may face as a Java EE developer and will show you how you should deal with them.
In this chapter, we will cover the following recipes:
  • Using CDI to inject context and dependency
  • Using Bean Validation for data validation
  • Using servlet for request and response management
  • Using Server Push to make objects available beforehand
  • Using EJB and JTA for transaction management
  • Using EJB to deal with concurrency
  • Using JPA for smart data persistence
  • Using EJB and JPA for data caching
  • Using batch processing

Using CDI to inject context and dependency

Context and Dependency Injection for Java EE (CDI) is one of the most important APIs under the Java EE umbrella. Introduced in Java EE 6, it now has a big influence over many other APIs.
In the recipe, you will learn how to use CDI in a couple of different ways and situations.

Getting ready

First, let's add the required dependency needed:
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>

How to do it...

  1. We are going to build a JAX-RS based application, so we will start by preparing the application to perform:
@ApplicationPath("webresources")
public class Application extends javax.ws.rs.core.Application {
}
  1. Then, we create a User application as our main object:
public class User implements Serializable {

private String name;
private String email;

//DO NOT FORGET TO ADD THE GETTERS AND SETTERS
}
Our User class doesn't have a default constructor, so CDI doesn't know how to construct the class when it tries to inject it. So, we create a factory class and use the @Produces annotation over its methods:
public class UserFactory implements Serializable{

@Produces
public User getUser() {
return new User("Elder Moraes", "[email protected]");
}

}
  1. Let's create an enumeration to list our profile types:
public enum ProfileType {
ADMIN, OPERATOR;
}
  1. Here, we create a custom annotation:
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
public @interface Profile {
ProfileType value();
}
  1. Add them to an interface to prototype the user profile behavior:
public interface UserProfile {
ProfileType type();
}
Now that we have defined the profile list and its behavior with respect to the user, we can give it a proper implementation for an admin profile:
@Profile(ProfileType.ADMIN)
public class ImplAdmin implements UserProfile{

@Override
public ProfileType type() {
System.out.println("User is admin");
return ProfileType.ADMIN;
}
}
And the same can be done for an operator profile:
@Profile(ProfileType.OPERATOR)
@Default
public class ImplOperator implements UserProfile{

@Override
public ProfileType type() {
System.out.println("User is operator");
return ProfileType.OPERATOR;
}
}
  1. Then, we create a REST endpoint by injecting all the objects that we are going to use into it:
@Path("userservice/")
@RequestScoped
public class UserService {

@Inject
private User user;

@Inject
@Profile(ProfileType.ADMIN)
private UserProfile userProfileAdmin;

@Inject
@Profile(ProfileType.OPERATOR)
private UserProfile userProfileOperator;

@Inject
private UserProfile userProfileDefault;

@Inject
private Event<User> userEvent;

...
  1. This method gets the user injected by CDI and sends it to the result page:
 @GET
@Path("getUser")
public Response getUser(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result", user);
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}
  1. This one does the same with an admin profile:
 @GET
@Path("getProfileAdmin")
public Response getProfileAdmin(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result",
fireUserEvents(userProfileAdmin.type()));
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}
  1. And this one does the same with an operator profile:
 @GET
@Path("getProfileOperator")
public Response getProfileOperator(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result",
fireUserEvents(userProfileOperator.type()));
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}
  1. Finally, we send the default profile to the result page:
 @GET
@Path("getProfileDefault")
public Response getProfileDefault(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result",
fireUserEvents(userProfileDefault.type...

Inhaltsverzeichnis