Java EE 8 Cookbook
Elder Moraes
- 382 pages
- English
- ePUB (adapté aux mobiles)
- Disponible sur iOS et Android
Java EE 8 Cookbook
Elder Moraes
Ă propos de ce livre
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
Foire aux questions
Informations
Server-Side Development
- 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
Getting ready
<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...
- 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 {
}
- 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
}
public class UserFactory implements Serializable{
@Produces
public User getUser() {
return new User("Elder Moraes", "[email protected]");
}
}
- Let's create an enumeration to list our profile types:
public enum ProfileType {
ADMIN, OPERATOR;
}
- Here, we create a custom annotation:
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
public @interface Profile {
ProfileType value();
}
- Add them to an interface to prototype the user profile behavior:
public interface UserProfile {
ProfileType type();
}
@Profile(ProfileType.ADMIN)
public class ImplAdmin implements UserProfile{
@Override
public ProfileType type() {
System.out.println("User is admin");
return ProfileType.ADMIN;
}
}
@Profile(ProfileType.OPERATOR)
@Default
public class ImplOperator implements UserProfile{
@Override
public ProfileType type() {
System.out.println("User is operator");
return ProfileType.OPERATOR;
}
}
- 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;
...
- 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();
}
- 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();
}
- 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();
}
- 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...