Book Image

Building RESTful Web Services with Java EE 8

By : Mario-Leander Reimer
Book Image

Building RESTful Web Services with Java EE 8

By: Mario-Leander Reimer

Overview of this book

Java Enterprise Edition is one of the leading application programming platforms for enterprise Java development. With Java EE 8 finally released and the first application servers now available, it is time to take a closer look at how to develop modern and lightweight web services with the latest API additions and improvements. Building RESTful Web Services with Java EE 8 is a comprehensive guide that will show you how to develop state-of-the-art RESTful web services with the latest Java EE 8 APIs. You will begin with an overview of Java EE 8 and the latest API additions and improvements. You will then delve into the details of implementing synchronous RESTful web services and clients with JAX-RS. Next up, you will learn about the specifics of data binding and content marshalling using the JSON-B 1.0 and JSON-P 1.1 APIs. This book also guides you in leveraging the power of asynchronous APIs on the server and client side, and you will learn to use server-sent events (SSEs) for push communication. The final section covers advanced web service topics such as validation, JWT security, and diagnosability. By the end of this book, you will have implemented several working web services and have a thorough understanding of the Java EE 8 APIs required for lightweight web service development.
Table of Contents (8 chapters)

Getting started with Java EE 8 microservices

In this section, we're going to take a look at the following things:

  • How to develop, build, and run your first Java-EE-8-powered microservice
  • Required Java EE 8 dependencies for basic web-service development
  • Basic components of any JAX-RS-based web service
  • Deployment of a thin WAR artifact using Payara Server 5

Let's get started and dive into the code. I've prepared my IDE and a raw skeleton Maven project. What you see here is a very basic POM file:

There's one thing missing though; first, we need to define the required dependency for the Java EE 8 API. Let's do that:

        <dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>

We specify version as 8.0. We should also define the proper scope for this, which is provided in this case because the Java EE 8 API will later be provided by our application server and we are done with our dependency. Next, we should add a beans.xml descriptor to the WEB-INF directory of our web application:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>

We do this and we're done, what's next? Well, next we should bootstrap our JAX-RS application. Now let's create a class called JAXRSConfiguration. The name really doesn't matter. What's important is that this class extends from the Application base class. Bear in mind the javax.ws.rs.core package while selecting the Application. It's also important that you specify the @ApplicationPath annotation. This will be the base path our REST API will be accessible under, thus we call that "api":

package com.packtpub.javaee8;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
* Configures a JAX-RS endpoint.
*/
@ApplicationPath("api")
public class JAXRSConfiguration extends Application {
}

Once we've bootstrapped JAX-RS, what's missing is a proper REST resource. Let's create a class called HelloWorldResouce. We used the @Path annotation, which will be the path this resource will be accessible under. We'll call that "hello". Next up, we create a method that will produce the proper response once called, we call that helloWorld. We use the proper Response here. We annotate this using the @GET annotation because we will be issuing GET requests later on, and we'll say that it produces MediaType.APPLICATION_JSON. Then we return Response.ok, where ok is HTTP status 200 of a response when we call the build. So, what should be used as the response? We'll be using Map<String, String> as our response and will return singletonMap with the message key and the Hello World value:

package com.packtpub.javaee8;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;

import static java.util.Collections.singletonMap;

/**
* The REST resource implementation class.
*/
@Path("hello")
public class HelloWorldResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response helloWorld() {
Map<String, String> response = singletonMap("message",
"Building Web Services with Java EE 8.");
return Response.ok(response).build();
}
}

We should already have a very simple working microservice. Now let's deploy this onto our Payara Server 5 and run it. We're going to deploy the WAR file, it's been built; you can see that it's already been deployed and the deployment took 5.1 milliseconds.

Let's check our browser. You should see the "Hello World." message, as shown in the following screenshot:

If you don't trust me, let's just modify the value here to "Building Web Services with Java EE 8." value. We deploy this once more and update our artifact. The new version has been deployed. Let's go back to our browser to check that we have the proper response message, as shown in the following screenshot:

That's all for this section; in the next section, I'm going to show you how to containerize your Java EE 8 microservice.