[Java] Now create a HelloWorld web application with Spring framework+Jetty

4 minute read

As the title says, I’ve had the opportunity to touch Java for the first time in 10 years, so I’ll study it myself. Since it is a study of the existing system that is in operation, it is a rather old combination, not the latest technology stack.

Do not deploy the web app, just run it locally.

Tools to use and knowledge level of the author.

  • Mac book Pro -It’s been about 6 months since I started using it. My personal life is almost Windows.
  • Java -Touch it for the first time in about 10 years. I’ve been touching it for about 3 years. Was Java 8 just announced at the time?
  • Maven -I touched it a long time ago. The main at that time was Ant. Is Gradle the mainstream now?
  • Spring framework -I know your name. There were many Struts at that time, but now it’s probably Spring boot.
  • IntelliJ IDEA Community -I know your name. At that time I was only using Eclipse.
  • Jetty Server -Java has never been used outside of Tomcat

Environmental preparation

I used Java as it was because it was included before. Java 8 not 14 or 11 due to adult circumstances.

$ java -version
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

For the time being, it’s mac, so I put Maven with brew.

$ brew install maven
(Omitted)
$ mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
(Omitted)
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"

Install IntelliJ

IntelliJ IDEA Community is also installed. The method is omitted because it was entered in the GUI.

Change the settings according to your preference. This was helpful. https://www.karakaram.com/intellij-idea-always-do-things/

Although I use the basic style as it is, it is not good for mental health, so I messed up with Keymap a little (especially C-h, C-d).

Create project

For now, create an empty project on GitHub and clone it. This is the working directory.

$ git clone https://github.com/sekikatsu36/spring-sample.git
$ cd spring-sample
$ git config --local user.name "sekikatsu36"
$ git config --local user.email "(short)"

Create a new Maven project with IntelliJ. Do not change the settings and leave the defaults.

Screenshot 2020-06-27 13.29.07.png

Screenshot 2020-06-27 13.36.29.png

This completes the initial state.

$ ls -a
. .DS_Store .gitignore pom.xml src
.. .git .idea spring-sample.iml

spring framework settings

There are many articles on spring boot on the Web, and there are few spring frameworks. However, spring framework is required now, so modify pom.xml to use it.

This is the complete form.

pom.xml


<?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>org.example.api</groupId>
    <artifactId>spring-sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <!-- Allow version to be handled variable in pom.xml -->
        <spring.version>5.2.5.RELEASE</spring.version>
        <jetty.version>9.4.11.v20180605</jetty.version>
    </properties>

    <dependencies>
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- jetty -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.14.v20181114</version>
            </plugin>
        </plugins>
    </build>
</project>

At the first stage, I wrote only properties and dependencies. Other points will be described later.

If an error occurs in the reference

In the case of my environment, I got an error that org.springframework.spring-webmvc:5.2.5.RELEASE could not be found. It exists in Repository.

Run

mvn clean install

result

  • Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-webmvc/5.2.5.RELEASE/spring-webmvc-5.2.5.RELEASE.jar is displayed in the log Was done-/Users/(user name)/.m2/repository/org/springframework/spring-webmvc/5.2.5.RELEASE is added
  • IntelliJ was still showing the error, but after reloading Maven the error disappeared

Write the source

Basically, just follow https://qiita.com/kasa_le/items/6aaf17823db67c951fb0. However, this article is IntelliJ IDEA Ultimate, so in the IDEA Community Create View, it does not move as it is.

For the time being, finish creating the controller and the view folder in this way. The state where the following is done.

/src/main
  /java/org/example/controllers
    IndexController.java
  /webapp/WEB-INF
    /views
    web.xml
    dispatcher-servlet.xml

Create a JSP file

In the case of Community edition, since there is no Spring support, it seems that JSP/JSPX does not appear when creating a new file. https://www.jetbrains.com/ja-jp/idea/features/editions_comparison_matrix.html

So, I usually choose File and make all my own.

index.jsp


<%@ page import="org.example.controllers.IndexController" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Spring Test Page</title>
    </head>
    <body>
        <p>${someAttribute}</p>
    </body>
</html>

Start with # Jetty

Use Jetty instead of Tomcat for the convenience of adults. If it is Ultimate (paid version) of IntelliJ IDEA, it seems that Tomcat is built in by default (?), but since it is a Community, it can not be used anyway. Jetty is easy because you don’t need to install it in advance.

Added the following to pom.xml with reference to http://zetcode.com/spring/jetty/.


   <packaging>war</packaging>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.14.v20181114</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

In this state

mvn jetty:run

And access http://0.0.0.0:8080, Hello World is displayed safely.

![Screenshot 2020-06-28 19.37.48.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/299048/a00b227c-282a-6173-b10e-(18b09c8a003d.png)

If you do not specify the source and target versions in maven-compiler-plugin, it will be 5, and an error will occur. http://orionb312.hatenablog.com/entry/2018/03/06/233734

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project spring-sample: Compilation failure: Compilation failure:
[ERROR] Source option 5 is not currently supported. Please use 7 or later.
[ERROR] Target option 5 is not currently supported. Please use 7 or later.

end

The finished product is below. https://github.com/sekikatsu36/spring-sample

For the time being, I was able to confirm that Spring works. Where I want to do a little more.

  • I want to use an API that is not a WebPage (use Jersey?)
  • I want to be able to debug (not stop at breakpoint now)
  • Want to use PaaS or SaaS