[JAVA] Create a Hello World web app with Spring framework + Jetty

As the title says, I had the opportunity to experience Java at work for the first time in 10 years, so I will study it myself now. It's a study of an existing system in operation, so it's not the latest tech stack, but a slightly older combination.

Don't deploy the web app, just run it locally.

The tools to use and the level of knowledge of the author.

Environmental preparation

Java was included before, so I'll just use it. Java 8 instead of 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)

Since it is mac for the time being, put Maven with brew.

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

IntelliJ installation

Also installed IntelliJ IDEA Community. Since I entered it with GUI, the method is omitted.

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

I basically use it as it is, but it's not good for mental health, so I just played with Keymap a little (especially C-h, C-d).

Project creation

For the time being, 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 "(Abbreviation)"

Create a new Maven project in IntelliJ. Leave the default settings without changing any settings.

スクリーンショット 2020-06-27 13.29.07.png スクリーンショット 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 about spring boot on the Web, and there are few articles about spring framework. However, I need spring framework now, so I modified pom.xml to make it usable.

This is the completed 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>

        <!--  pom.Allow the version to be handled as a variable in 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 beginning, I wrote only properties and dependencies. Other points will be described later.

If you get an error in the reference

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

Do the following

mvn clean install

result

--The log shows Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-webmvc/5.2.5.RELEASE/spring-webmvc-5.2.5.RELEASE.jar Was done --/Users/(username)/.m2/repository/org/springframework/spring-webmvc/5.2.5.RELEASE has been added --In IntelliJ, the error was still displayed, but when I reloaded Maven, the error disappeared.

Write the source

Basically, you can do it according to https://qiita.com/kasa_le/items/6aaf17823db67c951fb0. However, since this article is ʻIntelliJ IDEA Ultimate, in ʻIDEA Community, [Create View](https://qiita.com/kasa_le/items/6aaf17823db67c951fb0#view%E3%81%AE%E4%BD% After 9C% E6% 88% 90), it doesn't work as it is.

For the time being, the process of creating the controller and the view folder is completed in this way. 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, it seems that JSP / JSPX does not appear when creating a new file because there is no Spring support. https://www.jetbrains.com/ja-jp/idea/features/editions_comparison_matrix.html

So, normally select File and make everything yourself.

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. Ultimate (paid version) of IntelliJ IDEA seems to have Tomcat built in by default (?), But since it is a community, it can not be used anyway. Jetty doesn't need to be installed in advance, so it's easy.

With reference to http://zetcode.com/spring/jetty/, I added the following to pom.xml.


   <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

When you execute and access http://0.0.0.0:8080, Hello World is displayed safely.

スクリーンショット 2020-06-28 19.37.48.png

If you do not specify the source and target versions with maven-compiler-plugin, it seems to 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. 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 (now it doesn't stop at breakpoints) ――I want to use PaaS or SaaS

Recommended Posts

Create a Hello World web app with Spring framework + Jetty
Create a web api server with spring boot
Spring5 MVC Web App Development with Visual Studio Code Hello World Creation
Hello World with Spring Boot
Hello World with Spring Boot!
Hello World with Spring Boot
Hello, World! With Asakusa Framework!
Hello World (console app) with Apache Camel + Spring Boot 2
Create an app with Spring Boot 2
Until "Hello World" with Spring Boot
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Create PDF with itext7 ~ Hello World ~
[Swift] Create a project with Xcode (ver 12.1) and display "Hello, World!"
[Rails6] Create a new app with Rails [Beginner]
Create a simple web application with Dropwizard
Hello World with Eclipse + Spring Boot + Maven
Create a simple on-demand batch with Spring Batch
[Rails 5] Create a new app with Rails [Beginner]
How Spring Security works with Hello World
(IntelliJ + gradle) Hello World with Spring Boot
Create a Spring Boot app development project with the cURL + tar command
Spring Boot2 Web application development with Visual Studio Code Hello World creation
Create a website with Spring Boot + Gradle (jdk1.8.x)
Hello world! With Spring Boot (Marven + text editor)
Hello World at explosive speed with Spring Initializr! !! !!
Let's create a super-simple web framework in Java
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Show a simple Hello World with SpringBoot + IntelliJ
Try to display hello world with spring + gradle
Build a WEB system with Spring + Doma + H2DB
Create a Spring Boot development environment with docker
Hello World with Java Servlet and JSP (Easy web server startup with Maven + Jetty)
Hello World, a cross-platform GUI app with Groovy running on the Java platform
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
Hello World with Micronaut
Create a portfolio app using Java and Spring Boot
Build a WEB system with Spring + Doma + H2DB + Thymeleaf
Hello World (REST API) with Apache Camel + Spring Boot 2
Create a Chat app with WebSocket (Tyrus) + libGDX + Kotlin
Create Restapi with Spring Boot ((1) Until Run of App)
Create a simple demo site with Spring Security with Spring Boot 2.1
Hello World comparison between Spark Framework and Spring Boot
Build a WEB system with Spring + Doma + H2DB Part 2
[Rails] I tried to create a mini app with FullCalendar
The first WEB application with Spring Boot-Making a Pomodoro timer-
Hello World with VS Code!
Make a simple CRUD with SpringBoot + JPA + Thymeleaf ① ~ Hello World ~
I searched for a web framework with Gem in Ruby
Create a playground with Xcode 12
Until you create a Web application with Servlet / JSP (Part 1)
Create microservices with Spring Boot
Introducing Spring Boot2, a Java framework for web development (for beginners)
Hello World with SpringBoot / Gradle
Create a Spring Boot web app that uses IBM Cloudant and deploy it to Cloud Foundry
Create a simple web server with the Java standard library com.sun.net.httpserver
Let's make a book management web application with Spring Boot part1
Create a memo app with Tomcat + JSP + Servlet + MySQL using Eclipse
Let's make a book management web application with Spring Boot part3
Create a JVM for app distribution with JDK9 modules and jlink
Let's make a book management web application with Spring Boot part2