I decided to use Spring (Framework) at work I decided to touch Spring Boot, which I had been interested in for a long time. By the way, Spring Boot ≠ Spring Framework, but I dare to select Spring Boot in private.
This time, try using Spring CLI before Spring Boot.
software | version |
---|---|
OS | Windows10 Pro |
Java | OpneJDK 12.0.2 |
Spring CLI | v2.3.5.RELEASE |
Official introductory document (Japanese) Drop the spring-boot-cli-2.3.5.RELEASE-bin.zip
and unzip it.
Pass the spring-2.3.5.RELEASE \ bin
folder under the unzipped folder to the path.
Now you can use the spring command. Execute the following command as a trial, and if the version is returned, the installation is successful.
Version confirmation command
spring version
Execution result
C:\>spring version
Spring CLI v2.3.5.RELEASE
Write code for the REST API because it can be anywhere on your PC. The official introductory documentation is written in Groovy, but Java is fine too.
Java:【Java】app.java:
@RestController
public class Test {
@RequestMapping("/")
public String home() {
return "Hello World!";
}
@RequestMapping("/sb")
public String helloSb() {
return "Hello SpringBoot!";
}
}
groovy:【Groovy】app.groovy:
@RestController
class ThisWillActuallyRun {
@RequestMapping("/")
String home() {
"Hello World!"
}
@RequestMapping("/sb")
String helloSb() {
"Hello SpringBoot!"
}
}
By the way, if it is the above level, the import statement is unnecessary. (If you do it in the IDE, a compile error will be displayed)
Execute the following command in the place where the source code for REST API is located.
spring run app.java
Then, the following message is displayed and the REST API application is started. It seems that the necessary libraries are automatically downloaded.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.5.RELEASE)
2020-11-15 12:31:05.416 INFO 9532 --- [ runner-0] o.s.boot.SpringApplication : Starting application on XXXXXXXXXX(Machine name) with PID 9532 (started by xxxx in M:\develop\works\Spring\20201115_springboot_start)
2020-11-15 12:31:05.421 INFO 9532 --- [ runner-0] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (jar:file:/M:/develop/tools/Spring/spring-boot-cli-2.3.5.RELEASE-bin/spring-2.3.5.RELEASE/lib/spring-boot-cli-2.3.5.RELEASE.jar!/BOOT-INF/lib/groovy-2.5.13.jar!/) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2020-11-15 12:31:06.384 INFO 9532 --- [ runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-11-15 12:31:06.394 INFO 9532 --- [ runner-0] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-11-15 12:31:06.394 INFO 9532 --- [ runner-0] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39]
2020-11-15 12:31:06.426 INFO 9532 --- [ runner-0] org.apache.catalina.loader.WebappLoader : Unknown class loader [org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@6adca536] of class [class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader]
2020-11-15 12:31:06.458 INFO 9532 --- [ runner-0] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-11-15 12:31:06.458 INFO 9532 --- [ runner-0] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 889 ms
2020-11-15 12:31:06.601 INFO 9532 --- [ runner-0] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-15 12:31:06.879 INFO 9532 --- [ runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-15 12:31:06.887 INFO 9532 --- [ runner-0] o.s.boot.SpringApplication : Started application in 1.795 seconds (JVM running for 3.034)
Try accessing the following.
http://localhost:8080/
http://localhost:8080/sb
Oh ~ good ~: grinning:
The REST API has been created with haste only with the Spring CLI. (It just returns a string) You don't even need an IDE at this point. This may be fine if you want to make a really simple API mock.
Is Spring Boot proper next time? I want to make an application.
Official introductory document (Japanese)
Recommended Posts