• Complain

Mario-Leander Reimer [Mario-Leander Reimer] - Building RESTful Web Services with Java EE 8

Here you can read online Mario-Leander Reimer [Mario-Leander Reimer] - Building RESTful Web Services with Java EE 8 full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2018, publisher: Packt Publishing, genre: Home and family. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Mario-Leander Reimer [Mario-Leander Reimer] Building RESTful Web Services with Java EE 8

Building RESTful Web Services with Java EE 8: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Building RESTful Web Services with Java EE 8" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Learn the fundamentals of Java EE 8 APIs to build effective web services

Key Features
  • Design modern and stylish web services with Java EE APIs
  • Secure your web services with JSON Web Tokens
  • Explore the advanced concepts of RESTful web services and the JAX-RS API
Book Description

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.

What you will learn
  • Dive into the latest Java EE 8 APIs relevant for developing web services
  • Use the new JSON-B APIs for easy data binding
  • Understand how JSON-P API can be used for flexible processing
  • Implement synchronous and asynchronous JAX-RS clients
  • Use server-sent events to implement server-side code
  • Secure Java EE 8 web services with JSON Web Tokens
Who this book is for

If youre a Java developer who wants to learn how to implement web services using the latest Java EE 8 APIs, this book is for you. Though no prior knowledge of Java EE 8 is required, experience with a previous Java EE version will be beneficial.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Mario-Leander Reimer [Mario-Leander Reimer]: author's other books


Who wrote Building RESTful Web Services with Java EE 8? Find out the surname, the name of the author of the book and a list of all author's works by series.

Building RESTful Web Services with Java EE 8 — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Building RESTful Web Services with Java EE 8" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Implementing asynchronous web service clients

In this section, we're going to take a look at the basic usage of asynchronous JAX-RS client APIs. We're using InvocationCallback instances to react to completed and failed calls. We'll also see how to do invocation-chaining of asynchronous client requests with CompletableFuture.

Let's get started and switch to code. As usual, we prepared a small template project to get started. We'll be using a JUnit test to showcase the JAX-RS client APIs. We're going to set up the JAX-RS client instance and the JAX-RS webTarget instance for the previously implemented asynchronous service API. As you might remember, in the previous section, we used Fibonacci number calculations asynchronously. We'll rewrite the test using the asynchronous JAX-RS client APIs against our REST API.

Let's open the AsyncWebServiceClientIntegrationTest class and start our tests; the first test should be pretty easy. We want to construct an asynchronous request and we do this the way we did it before. We use webTarget.path and we request TEXT_PLAIN_TYPE. Now comes the real difference: we call the .async() method and then call .get(Long.class). As you can see, the return type of this call is Future. Let's rename it from longFuture to fibonacci and call the assertEquals method on that one:

@Test
public void fibonacci17() throws Exception {
Future fibonacci = webTarget.path("/fibonacci/17")
.request(MediaType.TEXT_PLAIN_TYPE).async()
.get(Long.class);
assertEquals(1597, (long) fibonacci.get());
}

This is pretty much all there is to using asynchronous APIs, though there is slightly more. You can register an invocation callback with get to be notified on completed and failed events. Then, we'll see how we implement those callbacks. As you can see, instead of calling get for the actual (Long.class) type, as we did previously, we call the get of InvocationCallback. We can implement the completed method for a successful execution and the failed method for a failure. Again, we'll return Future for a Fibonacci number and we can then call the get method on this Future:

@Test
public void fibonacci17WithCallback() throws Exception {
Future fibonacci = webTarget.path("/fibonacci/17")
.request(MediaType.TEXT_PLAIN_TYPE).async()
.get(new InvocationCallback() {
@Override
public void completed(Long aLong) {
LOGGER.log(Level.INFO,
"Completed Fibonacci 17 with {0}.", aLong);
}
@Override
public void failed(Throwable throwable) {
LOGGER.log(Level.WARNING,
"Completed Fibonacci 17 with error.", throwable);
}
});
assertEquals(1597, (long) fibonacci.get());
}

Finally, we'll see how to do invocation-chaining using CompletableFuture. This is quite interesting because we can chain several JAX-RS client calls using a CompletableFuture fluent API. Imagine we want to calculate the Fibonacci numbers 3, 4, 5, 6, 8, and 21 and do all that in one chain call. This is what it could look like:

@Test
public void fibonacci3_4_5_6_8_21() throws Exception {
CompletableFuture fibonacci =
Futures.toCompletable(webTarget.path("/fibonacci/{i}")
.resolveTemplate("i", 3)
.request(MediaType.TEXT_PLAIN_TYPE)
.async().get(Long.class))
.thenApply(i -> webTarget
.path("/fibonacci/{i}")
.resolveTemplate("i", i + 2)
.request(MediaType.TEXT_PLAIN_TYPE)
.get(Long.class))
.thenApply(i -> webTarget
.path("/fibonacci/{i}")
...
...
...
assertEquals(10946, (long) fibonacci.get());
}

As you can see, we do the first call and we use the .async() method, which returns Future. We'll convert this Future to a CompletableFuture, and then for the next calls, we use thenApply and we'll do that for the next one and so on and so forth. This will ultimately make seven calls.

Let's run this test to make sure everything's ready and it'll compile our tests. We can see that the first three are already successful; the Fibonacci49WithCallback should result in a 202, and then we're done.

That's all the magic behind the JAX-RS asynchronous line API, as shown in the following screenshot:

Output showing asynchronous tests running successfully Download the example - photo 1

Output showing asynchronous tests running successfully
Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata .
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Building-RESTful-Web-Services-with-Java-EE-8 . In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/ . Check them out!

Flexible JSON processing with JSON-P

In this section, we're going to take a look at using JSON-P builders to construct JSON arrays and objects. We'll see how you can use JSON-P in REST resources for marshalling and unmarshalling data, how to use JSON Pointers to access JSON structures, and have a closer look at JSON Patch and JSON Diff to modify JSON structures. We will also be using the @PATCH annotation and the application/json-patch+json content type to apply patches in our REST resources, so there's lots of content ahead.

Let's get started. As usual, we prepare a small REST resource as a template to start. The first thing we do is create arrays for JSON and JSON objects using the associated builders, so let's do that:

public void testJsonBuilder() {
JsonArray values = Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("aString", "Hello Json-P 1")
.add("aInteger", 42)
.add("aBoolean", false)
.add("aNullValue", JsonValue.NULL)
.build())

Here, we're using the createArrayBuilder to create an array builder, and we're using the add method to add JSON objects. Here, you can use Json.createObjectBuilder to obtain an object builder. On this object builder, we then call different add methods to add a string, an integer, a Boolean, or maybe a null value using the special JsonValue. That's all there is to it. Using these two builders, you can create complex JSON structures quite easily.

What do we do with this? The first thing we do is we return jsonArray; this is really straightforward. You can explicitly and directly return this jsonArray for marshalling. To do this, w e will produce an APPLICATION_JSON as our content type and JAX-RS will make sure that our jsonArray is serialized and marshalled to the corresponding JSON structure :

@Produces(MediaType.APPLICATION_JSON)
@GET
public JsonArray marshall() {
return jsonArray;
}

The same is true if we want to unmarshall the data using JSON-P. We will consume the APPLICATION_JSON, get the InputStream which is basically jsonBody, and we're going to use JsonReader Here, we're going to use Json.CreateReader(jsonBody) from the InputStream, obtain a JsonReader , and on the reader, we can read the array:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public void unmarshall(InputStream jsonBody) {
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Building RESTful Web Services with Java EE 8»

Look at similar books to Building RESTful Web Services with Java EE 8. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Building RESTful Web Services with Java EE 8»

Discussion, reviews of the book Building RESTful Web Services with Java EE 8 and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.