For people who start Spring Boot from scratch, I will write an article with the goal of moving something if I copy it for the time being. Basic Building a RESTful Web Service is used as a reference.
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.3
BuildVersion: 18D109
$ mvn --version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T03:41:47+09:00)
Maven home: /usr/local/Cellar/maven/3.6.0/libexec
Java version: 1.8.0_25, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.3", arch: "x86_64", family: "mac"
$
It's a so-called normal Mac, but if you have Maven, you can run it on Windows with almost the same procedure.
I will also communicate with curl.
$ curl --version
curl 7.54.0 (x86_64-apple-darwin18.0) ...
$
First, let's copy and paste pom.xml from the following.
$ mkdir myproject && cd $_
$ cat pom.xml
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<!-- Additional lines to be added here... -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Create a directory where you want to place the source and properties files.
$ mkdir -p src/main/java
$ mkdir -p src/main/resources
Create a file written by the magic that starts Spring Boot.
$ cat src/main/java/nu/mine/kino/springboot/SampleTomcatApplication.java
SampleTomcatApplication.java
package nu.mine.kino.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SampleTomcatApplication {
public static void main(String[] args) {
SpringApplication.run(SampleTomcatApplication.class, args);
}
}
Create the following Controller that describes the WEB function.
$ cat src/main/java/nu/mine/kino/springboot/GreetingController.java
GreetingController.java
package nu.mine.kino.springboot;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(
@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
Create a properties file that describes your preferences.
$ cat src/main/resources/application.properties
application.properties
server.compression.enabled: true
server.compression.min-response-size: 1
server.connection-timeout=5000
server.port=8080
server.address=0.0.0.0
server.port is the port number to start. If the default 8080 is acceptable, no description is actually required. server.address is a magical trick to connect from other machines.
Let's start it.
$ pwd
/xxxxx/xxx/myproject
$ mvn spring-boot:run
Various things are displayed,
...
2019-02-26 14:03:46.797 INFO 40644 --- [ main] n.m.k.s.SampleTomcatApplication :
Started SampleTomcatApplication in 4.749 seconds (JVM running for 11.845)
It's OK!
After starting, try communicating from another prompt.
$ curl http://localhost:8080/greeting
{"id":1,"content":"Hello, World!"}
$
It worked so well!
Stop the Spring Boot WEB server (Tomcat) that you started with Ctrl-C. Thank you for your support.
$ mvn clean package
Now you have an executable jar file that includes Tomcat.
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar
By doing so, Tomcat will start in the same way as the previous mvn spring-boot: run
. ..
$ mvn eclipse:clean eclipse:eclipse
Now that you have a .project / .classpath file, you can import it into Eclipse.
Add the following settings to application.properties.
$ cat src/main/resources/application.properties
application.properties
...
spring.jackson.serialization.indent-output=true
When I press Ctrl-C and restart with mvn spring-boot: run
and then connect with curl, ...
$ curl http://localhost:8080/greeting
{
"id" : 1,
"content" : "Hello, World!"
}
The JSON has been formatted. ..
-Spring Boot Official
Recommended Posts