I compared Hello, world! With Spring Boot with Java, Kotlin and Groovy, so make a note.
This article is based on the macOS environment.
Install Spring Boot CLI with Homebrew.
$ brew tap pivotal/tap
$ brew install springboot
If you execute the following command in the terminal, the version will be displayed. Note that if the Rails environment is local, the spring used by Rails may be executed, so adjust the PATH environment variable.
$ spring --version
Spring CLI v1.5.4.RELEASE
Create a Hello project with the following command. Use Gradle as the build tool, add the web as a dependency, use Java as the language, and extract it to the hello directory as a project named Hello.
$ spring init --build=gradle -d=web -l=java -n=hello hello
Code is generated in Kotlin and Groovy by changing -l = java
to -l = kotlin
or -l = groovy
.
You can edit the project code with a suitable text editor, but if you want to edit it with IntelliJ IDEA Ultimate or IntelliJ IDEA Community, open the project directory with the following command.
$ idea hello
Try implementing Hello, world! In Java, Kotlin and Groovy.
src/main/java/com/example/hello/HelloApplication.java
package com.example.hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
@RequestMapping("/")
String hello() {
return "Hello, world!";
}
}
src/main/kotlin/com/example/hello/HelloApplication.kt
package com.example.hello
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@SpringBootApplication
@RestController
class HelloApplication {
@RequestMapping("/")
fun hello() = "Hello, world!"
}
fun main(args: Array<String>) {
SpringApplication.run(HelloApplication::class.java, *args)
}
src/main/groovy/com/example/hello/HelloApplication.groovy
package com.example.hello
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@SpringBootApplication
@RestController
class HelloApplication {
static main(args) {
SpringApplication.run HelloApplication, args
}
@RequestMapping("/")
def hello() {
"Hello, world!"
}
}
There is not much difference with this kind of code.
Go to the Hello! Project directory and run the bootRun task in Gradle to start the web server.
$ ./gradlew bootRun
When I send a GET request to the launched Hello project, Hello, world! Is returned.
$ curl localhost:8080
Hello, world!
This time I tried Hello, world! With Spring Boot and Java / Kotlin / Groovy. I thought that Spring Boot would be a good micro framework at the beginning, but I tried it, but the result was that it lacked the feeling of micro because of the import statement. I think Spring Boot is a good environment where you can start from a microscopic place and evolve into a full stack, so I hope there will be more information that you can play with Spring without knowing anything.
https://docs.spring.io/spring-boot/docs/current/reference/html/
Recommended Posts