--Try to make a simple web application with Spring Boot ――In this article, first of all, something like Hello World -** Even people with no development experience can easily get started **
--One of the frameworks in Java development --The feature is that it requires less initial settings and is easy to start using.
environment | service/version |
---|---|
OS | Mac |
language | Java8 |
IDE(Integrated development environment) | IntelliJ IDEA |
Framework | SpringBoot 2.2.1 |
--Java must be installed in the development environment
--Web service that allows you to create a template application for Spring Boot --You can select the build tool (Maven / Gradle) and language (Java / Kotlin / Groovy).
Specify the following
--Build tool ――Both are commonly used, so either one is fine, but this time I will use Gradle. --Language --Spring Boot version
spring-study
--Library used
--Add Spring Web
(easy to build MVC application)
--Can be added later--Click the Generate button to download the template app as a zip
--Files such as SpringStudyApplication.java
and ʻapplication.properties are created by default --
SpringStudyApplicationTests.java` is unnecessary, so delete it
――If you keep the template, it won't work, so first of all, Hello World --When accessing http: // localhost: 8080 / hello, Hello World! Is displayed.
--Create a class that accepts requests and returns strings
HelloRestController.java
--Create HelloRestController.java
in com.examle.springstudy
com.examle.springstudy.HelloRestController.java
package com.example.springstudy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController // ①
public class HelloRestController {
@GetMapping("/hello") // ②
public String hello() {
return "Hello World!";
}
}
--Two annotations are the point (@ XXX
)
-①: Indicates that the controller accepts requests and returns responses.
-②: Associate the request URL with the logic
--Select the starting class (SpringStudyApplication) and execute (⌃ + ⌥ + R
)
--When started, the following contents are output to the console.
11:46:44: Executing task 'bootRun'...
> Task :compileJava
> Task :processResources
> Task :classes
> Task :bootRun
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.1.RELEASE)
2019-12-01 11:46:59.590 INFO 43520 --- [ main] c.e.springstudy.SpringStudyApplication : Starting SpringStudyApplication on MacBookPro with PID 43520 (/Users/tanibuchi/Desktop/spring-study/spring-study/build/classes/java/main started by tanibuchi.kosuke in /Users/tanibuchi/Desktop/spring-study/spring-study)
2019-12-01 11:46:59.595 INFO 43520 --- [ main] c.e.springstudy.SpringStudyApplication : No active profile set, falling back to default profiles: default
2019-12-01 11:47:00.598 INFO 43520 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-12-01 11:47:00.612 INFO 43520 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-12-01 11:47:00.612 INFO 43520 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-12-01 11:47:00.680 INFO 43520 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-12-01 11:47:00.680 INFO 43520 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1037 ms
2019-12-01 11:47:00.915 INFO 43520 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-01 11:47:01.089 INFO 43520 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-12-01 11:47:01.098 INFO 43520 --- [ main] c.e.springstudy.SpringStudyApplication : Started SpringStudyApplication in 2.169 seconds (JVM running for 3.005)
--If Started
is displayed at the end, it can be started normally.
--When you access http: // localhost: 8080 / hello, the following screen will be displayed.
――You can easily start application development using Spring Boot like this. ――This time, it is described at the summary level, so I will explain it a little more carefully next time.
Recommended Posts