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
Hello World with Spring Boot
Hello, World! With Asakusa Framework!
Hello World (console app) with Apache Camel + Spring Boot 2
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Create PDF with itext7 ~ Hello World ~
[Rails 5] Create a new app with Rails [Beginner]
How Spring Security works with Hello World
(IntelliJ + gradle) Hello World with Spring Boot
[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 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
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
(Intellij) Hello World with Spring Boot
(IntelliJ + gradle) Hello World with Spring Boot
Show a simple Hello World with SpringBoot + IntelliJ
Create a Spring Boot web app that uses IBM Cloudant and deploy it to Cloud Foundry
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
Create a restaurant search app with IBM Watson + Gurunavi API (with source)
Build a web application with Javalin
Create command line app with maven
Hello World with GlassFish 5.1 + Servlet + JSP
Try to create a server-client app
"Hello world" for ImageJ with Eclipse
Create exceptions with a fluid interface
Hello World with GWT 2.8.2 and Maven
Test Spring framework controller with Junit
Create a Maven project with a command
Try writing "Hello, World" with a combination of various languages and libraries
I want to create a dark web SNS with Jakarta EE 8 with Java 11
How to create a server executable JAR and WAR with Spring gradle
Create a LINEnews-style tech news summary app with Rails x LineBot! [Part 1]
Steps to create a simple camel app using Apache Camel Spring Boot starters
Create a java web application development environment with docker for mac part2
"Teacher, I want to implement a login function in Spring" ① Hello World
Create a simple bar chart with MPAndroidChart
Why spring consider as a lightweight framework
Start web application development with Spring Boot
Create a TODO app in Java 7 Create Header
[Practice! ] Display Hello World in Spring Boot
Java development with Codenvy: Hello World! Run
Create a temporary class with new Object () {}
"Hello, World!" With Kotlin + CLI in 5 minutes
I made a rock-paper-scissors app with kotlin
Deploy a Java web app on Heroku
Hello world with Kotlin and Tornado FX
Try with resources statement in web app