Giter Site home page Giter Site logo

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

View Code? Open in Web Editor NEW
1.0 3.0 5.0 60 KB

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

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

License: MIT License

Shell 52.63% Batchfile 40.72% Java 6.65%
packtpub packt javaee8 webservice microservice payara payara-micro

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

Building Web Services with Java EE 8
Section 1: Getting Started

This is the demo source code for the first section if the Packt Course Building Web Services with java EE 8. The Git repository contains several branches, each resembling the next step in the development of a simple Java EE 8 powered web service.

  • master: The final and working solution
  • step-00: The skeleton Maven project
  • step-01: Added Java EE 8 dependency to pom.xml
  • step-02: Added bean.xml file
  • step-03: Implemented JAX-RS configuration and REST resource
  • step-04: Added a Dockerfile

Video 1.4: Getting Started

In this video we will develop a basic Hello World web service using Java EE 8. The rough Maven skeleton project is contained in branch step-00.

Step 1

Next we need to add the Java EE 8 API dependency to our Maven POM (see branch step-01). Add the following snippet to the `pom.xml``file:

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

Step 2

In the next step we will add a beans.xml file to the src/main/webapp/WEB-INF directory of the project. This is so that our classes are recognized as CDI beans.

<?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_2_0.xsd"
       bean-discovery-mode="all">
</beans>

Step 3

In the final step we will add the JAX-RS configuration class as well as a simple REST resource class.

To start, add a class that extends javax.ws.rs.core.Application and specify the base path of the REST API using the @ApplicationPath annnotation.

@ApplicationPath("api")
public class JAXRSConfiguration extends Application {
}

Then implement a basic REST resource class like the following. Annotate the class with @Path and specify the path for this resource. Add a resource method for @GET and implement the body to return a response.

@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();
    }
}

Now you can build the project and deploy the final artifact to your Payara Server.

$ mvn clean package

Video 1.5: Containerization

To containerize our microservice, we have to write a Dockerfile. See the step-04 branch for the final result.

First, we need to decide which container base image to use. Have a look at Docker Hub. The two options presented here are:

  • Payara Server Full 5.0
  • Payara Micro 5.0

Add a Dockerfile to the base directory of this project and add the following content:

FROM payara/server-full:5.181
COPY target/hello-javaee8.war $DEPLOY_DIR

To build the image and run it as a container, open a command prompt and issue the following shell commands:

$ mvn package

$ docker build -t hello-javaee8:1.0-full .
$ docker run -it -p 8080:8080 hello-javaee8:1.0-full

You should now be able to access the web service under the IP of you Docker host.

Next, we want to use the Payara Micro base image instead. Add the following content to the Dockerfile:

FROM payara/micro:5.181
COPY target/hello-javaee8.war /opt/payara/deployments

To build the image and run it as a container, open a command prompt and issue the following shell commands:

$ mvn package

$ docker build -t hello-javaee8:1.0-micro .
$ docker run -it -p 8080:8080 hello-javaee8:1.0-micro

Again, you should now be able to access the web service under the IP of you Docker host.

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

Contributors

lreimer avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.