Having the basic of a REST based Java app


A Java developer working with web app back-end development is required to have JavaEE development where the program design follows the RESTful architecture. To develop a RESTful application, follow useful tutorials and descriptions for JavaEE, choice of ServletContainers, framework description for developing RESTful web services such as Jersey, annotations that are used in a Java class for a JavaEE REST app, and the servlet naming and mapping in the project’s web.xml file. Some good tutorials that were beneficial to me were:

And some useful descriptions were:

At the time of preparing the Java RESTful app one should consider that the project’s Maven pom.xml file has the correct dependencies, as well as a right way to reference the application’s servlets in the web.xml file. One of the Maven dependencies that works as a ServletContainer and is the base for implementing a “Jax-RS with Jersey” is the jersey-container-servlet:

       <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.29.1</version>
        </dependency>

The content of the web.xml file, which reference the Servlet being developed would then be:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.javatpoint.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

The good tutorial at https://www.javatpoint.com/jax-rs-example-jersey has a more complete description.

If an error is encountered when deploying the application to the application server, one can search on StackOverflow and with links such as:



Maven pom.xml file structure for an application

When writing a Java application that utilizes library packges developed by others one can form the Maven building and managing system. A Maven project organizes your Java application’s Jar file requirements into an accessible structure and will help build your application when you have finished developing the application. In order for Maven to have a list of Jar files used in the application, Maven saves these references and information in a file called pom.xml.

A pom.xml file has references for example to how you want to package your application (for example jar or war), the plug-ins that help build the application and the jar libraries.

The jar libraries are included in the “dependecies” part of the pom.xml file and are organized into a code format:

<dependency>
  <groupId> name of group </groupId>
  <artifactId> name of artifact </artifactId>
  <version> version number </version>
</dependency>

  • Here is one pom.xml file for a JavaEE web app that is used to develop a servlet, which has a MySQL database connection within the servlet:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mygrouptasks</groupId>
    <artifactId>assistancesDescription</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
    <finalName>assistDescrip</finalName>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.1.0</version>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
        </plugin>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </plugin>

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
        </plugin>

        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.2</version>
        </plugin>

        <plugin>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
        </plugin>

        <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.8.2</version>
        </plugin>

    </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>
    </dependencies>
</project>