[Java] Create a website with Spring Boot + Gradle (jdk1.8.x)

3 minute read

What is this?

How do you make a website in Java? That is, the first step is to display only one HTML page. This article is aimed at operating a web server and displaying it on a browser. If it works, I think it’s good to remember it while making corrections.

Confirmed system requirements

In August 2020, we have confirmed the operation with the following requirements.

  • macOS 10.14.6
  • Homebrew 2.4.12
  • Gradle 6.6
  • jdk1.8.x
  • git

The sample code used here can be found on GitHub.

1. Procedure: First try moving

1.1. Create and move Project directory

Terminal


$ mkdir -p helloworld; cd $_

1.2. Initialize Gradle Project

Terminal


$ gradle init

From here, proceed interactively, so select as follows.

  1. Select type of project to generate » 2: application
  2. Select implementation language » 3: Java
  3. Select build script DSL » 1: Groovy
  4. Select test framework » 1: JUnit 4
  5. Project name (default: helloworld) » helloworld
  6. Source package (default:) » helloworld

This completes the initialization of Gradle Project.

1.3. Clone the sample code

Terminal


$ git clone https://github.com/ryoyakawai/java_gradle_springboot_helloworld.git

1.4. Start the web server

Terminal


$ cd java_gradle_springboot_helloworld
$ gradle bootRun

When started without error, the following should be displayed in Terminal.

Terminal


> Task :bootRun

  . ____ _ __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
(( )\___ |'_ |'_| |'_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| |) ))))
  '|____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot :: (v2.2.0.RELEASE)

2020-08-18 15:47:55.672 INFO 64412 --- [main] cehelloworld.HelloworldApplication: Starting HelloworldApplication on S1031198.local with PID 64412 (/..../java_gradle_springboot_helloworld/build/classes/java/main started by ryoya.kawai in /..../java_gradle_springboot_helloworld)
2020-08-18 15:47:55.674 INFO 64412 --- [main] c.e.helloworld.HelloworldApplication: No active profile set, falling back to default profiles: default
2020-08-18 15:47:56.180 INFO 64412 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer: Tomcat initialized with port(s): 8080 (http)
2020-08-18 15:47:56.187 INFO 64412 --- [main] o.apache.catalina.core.StandardService: Starting service [Tomcat]
2020-08-18 15:47:56.187 INFO 64412 --- [main] org.apache.catalina.core.StandardEngine: Starting Servlet engine: [Apache Tomcat/9.0.27]
2020-08-18 15:47:56.228 INFO 64412 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/]: Initializing Spring embedded WebApplicationContext
2020-08-18 15:47:56.229 INFO 64412 --- [main] o.s.web.context.ContextLoader: Root WebApplicationContext: initialization completed in 527 ms
2020-08-18 15:47:56.323 INFO 64412 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor: Initializing ExecutorService'applicationTaskExecutor'
2020-08-18 15:47:56.422 INFO 64412 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer: Tomcat started on port(s): 8080 (http) with context path ``
2020-08-18 15:47:56.424 INFO 64412 --- [main] c.e.helloworld.HelloworldApplication :Started HelloworldApplication in 1.139 seconds (JVM running for 1.368)
<=========----> 75% EXECUTING [10s]
> :bootRun

Check the operation from the browser here.

If you access http://localhost:8080 with a browser and the following is displayed, it means that the server is operating normally. sample00

2. Content description

2.1 About files

It is the file structure immediately after Clone in “1.3. Clone the sample code”. After the “1.2. Initialize Gradle Project” implementation, I put my hand on the part of the file and directory where the explanation is written on the right side of the figure below.

├── build.gradle (required modification: Gradle configuration file)
├── settings.gradle
└── src
    ├── main
    │ ├── java
    │ │ └── helloworld (The following must be fixed: Program code)
    │ │ ├── HelloworldApplication.java
    │ │ ├── HelloworldController.java
    │ │ ├── HelloworldErrorController.java
    │ │ └── HelloworldServletInitializer.java
    │ ├── resources
    │ │ ├── application.yml (required modification)
    │ │ ├── static
    │ │ │ └── assets (the following is new creation)
    │ │ │ └── sample-300x300.jpg
    │ │ └── templates
    │ │ ├── error.html (required correction: HTML)
    │ │ └── helloworld.html (required modification: HTML)
    │ └── webapp
    │ └── WEB-INF
    │ └── appengine-web.xml
    └── test
        └── java
            └── hello world
                └── HelloworldApplicationTests.java (Correction required: test file)

2.2. Explanation of each file

  • build.gradle: Write Build settings. You will need to make corrections as needed.
  • settings.gradle: Describe the project name which is the entry point.

src> main >java >helloworld The following files

  • HelloworldApplication.java: Declares to use Spring Boot framework.
  • HelloworldController.java: Controller class. It mainly describes what path to access and what to display.
  • HelloworldErrorController.java: This class handles application errors. It needs to be modified accordingly.
  • HelloworldServletInitializer.java: WebApplicationInitializer implementation class required in the environment to deploy and operate the WAR file. (Although it exists, it is not used in the operation here and is not necessary originally)

src> main >resources The following filesPlace HTML files etc. in this directory.

  • application.yml: YML file that defines the message.
  • templates >error.html: HTML to display when an error occurs.
  • templates >helloworld.html: HTML that is displayed during normal operation.
  • static> assets: Images should be placed here.

References