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

What is this?

How do you create a website in Java? In the first step, the deliverable will only display one HTML page. This article aims to operate a web server and display it on a browser. If it works, I think it's a good idea to make corrections and remember it.

Confirmed system requirements

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

The sample code used here can be found on GitHub.

1. Procedure: First try to move

1.1. Create and move the Project directory

Terminal


$ mkdir -p helloworld; cd $_

1.2. Initialize Gradle Project

Terminal


$ gradle init

From here, you can 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 the Gradle Project.

1.3. Clone the sample code

Terminal


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

1.4. Try starting the web server

Terminal


$ cd java_gradle_springboot_helloworld
$ gradle bootRun

If you start it without any errors, you should see the following in Terminal:

Terminal


> Task :bootRun

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

2020-08-18 15:47:55.672  INFO 64412 --- [           main] c.e.helloworld.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 web server is operating normally. sample00

2. Description of contents

2.1 About files

This is the file structure immediately after cloning in "1.3. Clone sample code". I made some changes after executing "1.2. Initialize Gradle Project" in the part where the explanation is written on the right side of the file and directory in the figure below.

├── build.gradle (Must be modified: Gradle config file)
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── helloworld  (The following must be modified: Program code)
    │   │       ├── HelloworldApplication.java
    │   │       ├── HelloworldController.java
    │   │       ├── HelloworldErrorController.java
    │   │       └── HelloworldServletInitializer.java
    │   ├── resources
    │   │   ├── application.yml (Must be modified)
    │   │   ├── static
    │   │   │   └── assets  (The following is newly created)
    │   │   │       └── sample-300x300.jpg
    │   │   └── templates
    │   │       ├── error.html (Must be modified: HTML)
    │   │       └── helloworld.html (Must be modified: HTML)
    │   └── webapp
    │       └── WEB-INF
    │           └── appengine-web.xml
    └── test
        └── java
            └── helloworld
                └── HelloworldApplicationTests.java  (Must be modified: test file)

2.2. Description of each file

--build.gradle: Write the Build settings. Modifications are required as necessary. --settings.gradle: Describe the project name that will be the entry point.

src> main> java> helloworld The following files

--HelloworldApplication.java: Declares to use the Spring Boot framework. --HelloworldController.java: Controller class. It mainly describes what Path is accessed and what is displayed. --HelloworldErrorController.java: A class that handles application errors. It needs to be modified accordingly. --HelloworldServletInitializer.java: WebApplicationInitializer implementation class required in the environment where the WAR file is deployed and operated. (Although it exists, it is not used in the operation here and is not necessary originally)

src> main> resources The following files

Place HTML files etc. in this directory.

--ʻApplication.yml: The YML file that defines the message. --templates> error.html: HTML to be displayed when an error occurs. --templates> helloworld.html: HTML to be displayed during normal operation. --static> assets`: Images should be placed here.

References -Google App Engine (Java 8) + Spring Boot + Gradle with Hello World --Qiita ――We are very helpful. Thank you very much. ―― What do you need after all to make a web application using java? A quick explanation of how it works and what you need for learning --Qiita --Explanation up to the framework (Spring Boot).

Recommended Posts

Create a website with Spring Boot + Gradle (jdk1.8.x)
Create a simple search app with Spring Boot
Create a web api server with spring boot
Create a Spring Boot development environment with docker
Create microservices with Spring Boot
Create a simple demo site with Spring Security with Spring Boot 2.1
Create an app with Spring Boot 2
Create an app with Spring Boot
Spring Boot gradle build with Docker
Create a simple on-demand batch with Spring Batch
(IntelliJ + gradle) Hello World with Spring Boot
Create a Spring Boot app development project with the cURL + tar command
Run Scala applications with Spring Boot through Gradle
Create a Spring Boot application using IntelliJ IDEA
Build Spring Boot project by environment with Gradle
Java tips-Create a Spring Boot project in Gradle
Create CRUD apps with Spring Boot 2 + Thymeleaf + MyBatis
How to create a server executable JAR and WAR with Spring gradle
Create your own Utility with Thymeleaf with Spring Boot
I wanted to gradle spring boot with multi-project
Create Spring Boot environment with Windows + VS Code
Download with Spring Boot
Create Spring Cloud Config Server with security with Spring Boot 2.0
Create a portfolio app using Java and Spring Boot
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
Implement a simple Rest API with Spring Security with Spring Boot 2.0
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
A memorandum when creating a REST service with Spring Boot
Create Restapi with Spring Boot ((1) Until Run of App)
I wrote a test with Spring Boot + JUnit 5 now
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Spring Boot 2 multi-project in Gradle
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Create a playground with Xcode 12
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Send email with spring boot
A story packed with the basics of Spring Boot (solved)
Create a Hello World web app with Spring framework + Jetty
Let's make a simple API with EC2 + RDS + Spring boot ①
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Create a Vue3 environment with Docker!
Use Basic Authentication with Spring Boot
Build a Java project with Gradle
◆ Spring Boot + gradle environment construction memo
gRPC on Spring Boot with grpc-spring-boot-starter
Hot deploy with Spring Boot development