--Create a simple CRUD app with Spring Boot ――This time, make a template and check the operation
-You can drop the zip at here, but this time it is created from the STS plugin. --For Eclipse The STS plugin can be downloaded from the Eclipse marketplace in the help
--Creating a template
--Since it is a sample, the app name can be anything.
--Both maven and gradle are often used as build tools, but this time I chose maven ――It doesn't matter which one you choose in the range you make this time
――If you select the required library, you can create a template in the bundled state. --This time, select the following 5 (can be added later) --Devtools ・ ・ ・ You will be able to do hot deployment --JPA ・ ・ ・ ORMapper that connects Java and DB --H2 ・ ・ ・ In-memory DB --Thymeleaf ・ ・ ・ Template engine --Web ・ ・ ・ You can easily create an endpoint (URL)
――When you finish generating the template, first check the operation --It is convenient to use the Boot dashboard to start the application.
--If the Boot dashboard is not displayed, you can add it by following the steps below.
--When you start it, the output like this is output to the console
--If it says started at the end, the startup is completed normally.
--You can also start the app on the terminal with the following command
- mvn spring-boot:run
BaseballApplication.java
src/main/java/com/example/baseball/BaseballApplication.java
package com.example.baseball;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BaseballApplication {
public static void main(String[] args) {
SpringApplication.run(BaseballApplication.class, args);
}
}
--This main method is the entry point of the app and launches the Spring app.
application.properties
src/main/resources/application.properties
--Empty file at this point --Describe settings related to the app
BaseballApplicationTests.java
src/test/java/com/example/baseball/BaseballApplicationTests.java
package com.example.baseball;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BaseballApplicationTests {
@Test
public void contextLoads() {
}
}
--Test code template --Tests can be run from the menu below
--Hello World is displayed when accessing http: // localhost: 8080 /
--Two files to create --The first is a file that receives access from the user and returns a template --The second is a template to return to the user
HelloWorld.java
--Create HelloWorld.java
in com.example.baseball
src/main/java/com/example/baseball/HelloWorld.java
package com.example.baseball;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller // ①
public class HelloWorld {
@GetMapping("/") // ②
public String hello() {
return "hello"; // ③
}
}
-①: By adding @Controller
, this Class can receive access from the user.
-②: If you set @GetMapping ("/ ")
, this method will be called when the http method is GET and the URL is /
.
-The path in () represents the content following http: // localhost: 8080
--For example, @GetMapping ("/ users ")
will be mapped to http: // localhost: 8080 / users
.
-③: Returns hello.html
undersrc / main / resources / templates /
to the user
--The reason why it behaves like that even though it just returns a String is because this Class has @Controller
.
hello.html
--Create hello.html
insrc / main / resources / templates /
src/main/resources/templates/hello.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>baseball</title>
</head>
<body>
<h1>HelloWorld</h1>
</body>
</html>
--When you access http: // localhost: 8080 /, the following screen will be displayed.
--If you add / modify a file while the app is running, the app will be restarted automatically every time the changes are saved. --Because I put devtools when generating the template, it works like this
--Continued here
Recommended Posts