Git Product home page Git Product logo

lreimer / packtpub-building-javaee8-webservices-section.2 Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 2.0 96 KB

Building Webservices with Java EE 8 - Packt Publishing Video Course Section 2

Home Page: https://www.packtpub.com/application-development/building-web-services-java-ee-8-video

License: MIT License

Java 98.96% Dockerfile 1.04%
packtpub packt javaee8 payara payara-micro webservice microservice

packtpub-building-javaee8-webservices-section.2's Introduction

Building Web Services with Java EE 8
Section 2: Building Synchronous Web Services

Videos

Video 2.1: Basic REST API

In this video we are going to implement a basic REST API for books.

Method URI Status Description
GET /api/books 200 Get the list of all books
POST /api/books 201 Create a new book from JSON body
PUT /api/books/{isbn} 200 Update a book with given ISBN
DELETE /api/books/{isbn} 200 Delete a book with given ISBN

Video 2.2: Subresource Locators

In this video we are extending the book API. We add loans for books using a sub resource (since loans are dependent on books).

Method URI Status Description
GET /api/books/{isbn}/author 200 Get the author for given book
GET /api/books/{isbn}/loans 200 Get the loans for given book
POST /api/books/{isbn}/loans 201 Create a new loan on a given book
DELETE /api/books/{isbn}/loans/{loanId} 200 Delete a loan and return given book

Now there are several ways to implement Subresource locators. In all cases you can perform partial processing by annotating methods using @Path only. The return type will then be scanned for further JAX-RS annotations.

Video 2.3: Error Handling

For REST APIs it is considered best practice to signal errors and abnormal behaviour using HTTP Status code. This includes expected as well as unexpected exceptional behaviour.

There are a couple of options you can choose from:

  • Handle the error explicitly, by checking or catching an exception and then set a suitable HTTP status code on the Response object. This could be a 404 if something could not be found.
  • The other alternative you have is to throw WebApplicationException or one of its subclasses. JAX-RS will handle these and translate them to the appropriate HTTP status code.
  • The final and most flexible option is to implement the ExceptionMapper interface and perform a custom mapping from exception to Response.

Video 2.4: JAX-RS Client API

In this video we add a small standalone CLI client program that uses the JAX-RS Client APIs to access the Library Service REST API. The client performs several CRUD interactions with Book and Loan.

Containerization

When you want to use the Payara Server Full base image, write the following Dockerfile:

FROM payara/server-full:5.181
COPY target/library-service.war $DEPLOY_DIR

When you want to use the Payara Micro base image, write the following Dockerfile:

FROM payara/micro:5.181
COPY target/library-service.war /opt/payara/deployments
$ docker build -t library-service:1.0 .

$ docker run -it -p 8080:8080 library-service:1.0
$ docker run -d -p 8080:8080 library-service:1.0

packtpub-building-javaee8-webservices-section.2's People

Contributors

lreimer avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kgiove diegoarei

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.