[JAVA] Challenge Spring Boot

Introduction

"It's okay to make it an app already"

With that said, we will take on the challenge of upgrading the Othello program, which could only be executed on the console, to a Web application. This time, we will create a sample level project using Spring Boot, which is said to be easy to build a web application in the Spring Framework.

In addition, there is almost no technical explanation, and the procedure of the actual work is described as a memorandum. I referred to various books and websites, but ~~ Black magic ~~ Since there is no black box feeling, I will proceed with the policy of "just move" as with Git related.

Create a new Spring Boot project in Eclipse

There seem to be various ways to create a Spring Boot project, but since I have been developing in Eclipse so far, I will take the method of additionally installing STS (Spring Tool Suite) in Eclipse.

Flow of installing and setting STS (Spring Tool Suite) in Eclipse

  1. From the menu bar of Eclipse (Oxygen.3a this time), go to Help → Eclipse Marketplace.
  2. Enter STS in the search field and click Search.
  3. Select Spring Tools (3.9.5 this time) from the search results.
  4. Press "Install" → press "Confirm" → select "I accept the terms of the terms of use" and press "Finish".
  5. The installation will start. You will be asked if you want to accept something on the way, so accept it. (I impulsively clicked the button, so I don't know what it was ...)
  6. After installation, you will be prompted to restart Eclipse. When you restart, the overview screen will appear. Click on the workbench.
  7. From the menu bar, go to Window → Perspective → Open Perspective → Other.
  8. Select Spring, then click Open.

Flow until creating a new Spring project

  1. Select Spring in the perspective.
  2. From the menu bar, go to File → New → Spring Starter Project.
  3. Enter MyOthelloApp as the project name, Gradle (Buildship 2.x) as the type, 8 as the Java version, com.myothello as the group name, MyOthello project as the description, and com.myothello as the package name, and click Next.
  4. Select the web and click Next.
  5. Click Finish.

Check the source code

src / main / java folder → MyOthelloAppApplication.java is created in the com.example.demo folder.

MyOthelloAppApplication.java (click to open)
package com.myothello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyOthelloAppApplication {

	public static void main(String[] args) {
		SpringApplication.run(MyOthelloAppApplication.class, args);
	}

}

The line starting with @ above the MyOthelloAppApplication class is called annotation, which specifies various roles in the app. The Spring Boot Application above means that it is an application that starts with Spring Boot.

I will try it. When I right-clicked on the name of MyOthelloApp and ran it as a Spring Boot application, the following message (excerpt) appeared on the console.

***************************
APPLICATION FAILED TO START
***************************

Description:

The Tomcat connector configured to listen on port 8080 failed to start. 
The port may already be in use or the connector may be misconfigured.

Since Tomcat has already used port 8080, it's unpleasant, so I'll ask Google University teacher. A page that says that you should kill the process using this 8080 (here I found tomcat-connector-configured-to-listen-on)), so I will refer to it here. From the command prompt

netstat -ao | find "8080"

Enter. Then, the PID using 8080 was searched, and 10572 came out this time, so next

Taskill /PID 10572 /F

Enter. Then, it is said that it was forcibly terminated, so when I tried it again, it was no longer said that APPLICATION FAILED TO START. If you access the browser by typing "localhost: 8080" in this state,

HelloOthello_Whitelabel.png

Was displayed. Since we haven't decided on the content of the page (Controller) yet, the 404 error is natural. This is OK now.

For the time being, "Hello othello"

Let's display "Hallo world" and "Hello othello" in the web application.

Web apps in Spring Boot are designed with the MVC architecture. M stands for Model (data used in the application), V stands for View (screen display), and C stands for Controller (process control). First, create a Controller.

Creating the MyOthelloAppController class

Create MyOthelloAppController.java in the same directory as MyOthelloAppApplication.java. At that time, check "Inherited abstract method". Next, write the code that displays "Hello othello" in the MyOthelloAppController class.

MyOthelloAppController class (click to open)
package com.myothello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyOthelloAppController {
	@RequestMapping("/")
	public String hello() {
		return "Hello othello";
	}
}

If you run it again and access 8080 ...

HelloOthello.png

I did it.

Future tasks

With that said, we are ready to develop an Othello program that runs on the Web, the original Othello app (probably). We will use various classes created for the console so far, and first implement Controller mainly.

Thank you for reading!

Recommended Posts

Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
SPRING BOOT learning record 01
Spring Boot + Heroku Postgres
Spring boot memo writing (1)
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Spring Boot learning procedure
Learning Spring Boot [Beginning]
Spring boot memo writing (2)
Spring Boot 2.2 Document Summary
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Download with Spring Boot
[Spring Boot] Environment construction (macOS)
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot
Hello World with Spring Boot
Spring Boot on Microsoft Azure
Implement GraphQL with Spring Boot
Spring Boot tutorial task schedule
Spring 5 & Spring Boot 2 Hands-on preparation procedure
Get started with Spring boot
Spring Boot 2 multi-project in Gradle
[Spring Boot] Web application creation
spring boot port duplication problem
Run LIFF with Spring Boot
SNS login with Spring Boot
Spring Boot Hot Swapping settings
Introduction to Spring Boot ① ~ DI ~
File upload with Spring Boot
Spring Boot starting with copy
Introduction to Spring Boot ② ~ AOP ~
CICS-Run Java application-(4) Spring Boot application
Spring Boot starting with Docker
Spring Boot + Springfox springfox-boot-starter 3.0.0 Use
Spring Boot DB related tips
Hello World with Spring Boot
Set cookies with Spring Boot
[Spring Boot] Easy paging recipe
Use Spring JDBC with Spring Boot
Docker × Spring Boot environment construction
Major changes in Spring Boot 1.5
Add module with Spring Boot
Getting Started with Spring Boot
NoHttpResponseException in Spring Boot + WireMock
[Spring Boot] Send an email
Spring Boot performance related settings
Introduction to Spring Boot Part 1
Spring Boot External setting priority
Try using Spring Boot Security
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
Try Spring Boot on Mac
Create microservices with Spring Boot
Spring Boot DB connection pool