[JAVA] Learning Spring Boot [Beginning]

I've been touching Java for a while at work, but I haven't had a chance to touch the trendy frameworks, but when I look at Spring Boot somehow, maybe this time it will take root as a standard framework. What? There is no loss to remember. I decided to touch it.

[Starter Project]

This is my first Spring Boot project creation, but since there is no other choice, it may be a standard, but I will start from here.

    1. Project setting 1 Gradle is easy for me to see the settings, so I will use it. The package is war because I don't know the jar yet.

start_1.png

  1. Project setting 2 For the time being, I just want to move it, so I chose only the template engine and the web Maybe I can add it later. start_2.png

    1. Contents of automatic generation It was automatically generated as shown below. start_3.png
  2. Creating a controller Controller is mainly used as a controller for Web pages. RestController is used in the controller for WebAPI that returns Json, XML, etc. However, I would like to try both. First, create it from RestController.

TestRest.java


package com.example.controller;

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

@RestController
public class TestRest {

	@RequestMapping("/Rest")
	public String Rest() {
		return "This is Rest";
	}

}

start_4.png

  1. Try to run Select "Spring Boot Application" in Run start_5.png

  2. Try to access from a browser It seems that Tomcat is working, but can't find the controller I created? What on earth do you mean? start_6.png

  3. Try moving the RestController to the package where the Spring Boot Application is located start_7.png

  4. Access from the browser again For some reason, it worked! Does that mean that you can only create a controller under the Spring Application? start_8.png

  5. I will try to create a package under Spring Application.

TestRest.java


package com.example.demo.controller;

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

@RestController
public class TestRest {

	@RequestMapping("/Rest")
	public String Rest() {
		return "This is Rest. From the package under demo";
	}

}

After all, that's right. start_9.png Anyway, my understanding has improved a little. RestController should be about this. Proceed to the next. Let's use the template engine with Controller.

10. Let's make a Controller.

Create index.html in the "templates" folder.

index.html


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>First Thymeleaf</title>
</head>
<body>
 <h1 th:text="${message}"></h1>
</body>
</html>

Create "TestController.java" in com.example.demo.controller.

TestController.java


package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

	@RequestMapping("/")
	public String index(Model model) {
		model.addAttribute("message", "Thymeleaf has moved!");
		return "index";
	}

}

start_10.png

  1. Execute and check the operation of templates It worked! I'm happy. You can come to this point smoothly without being conscious of anything about the character code and without garbled characters. Spring Boot looks pretty good. Next, let's make a form. start_11.png

Recommended Posts

Learning Spring Boot [Beginning]
SPRING BOOT learning record 01
Spring Boot learning procedure
Spring Boot for annotation learning
Beginning with Spring Boot 0. Use Spring CLI
Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
Spring Boot + Heroku Postgres
Spring boot memo writing (1)
First Spring Boot (DI)
Spring Boot2 cheat sheet
Spring Boot Servlet mapping
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)
Set context-param in Spring Boot
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
Get started with Spring boot
Hello World 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
[Java] Thymeleaf Basic (Spring Boot)
Introduction to Spring Boot ① ~ DI ~
File upload with Spring Boot
Spring Boot starting with copy
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