Spring Boot starting with copy

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.

environment

$ 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) ...
$

Try

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.

Start-up

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.

bonus

Jar

$ 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. ..

Load with Eclipse

$ mvn eclipse:clean eclipse:eclipse

Now that you have a .project / .classpath file, you can import it into Eclipse.

Format the JSON of the return value and return it

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. ..

Related Links

-Spring Boot Official

Recommended Posts

Spring Boot starting with copy
Spring Boot starting with Docker
Download with Spring Boot
Generate barcode with Spring Boot
Hello World with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
[Java] [Spring Boot] Specify runtime profile --Spring Boot starting with NetBeans
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Hot deploy with Spring Boot development
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Get validation results with Spring Boot
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Spring Boot starting from zero Part 2
Spring Boot starting from zero Part 1
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
Processing at application startup with Spring Boot
Hello World with Eclipse + Spring Boot + Maven
Send regular notifications with LineNotify + Spring Boot
Perform transaction confirmation test with Spring Boot
HTTPS with Spring Boot and Let's Encrypt
Challenge Spring Boot
Try using Spring Boot with VS Code
Start web application development with Spring Boot
Launch Nginx + Spring Boot application with docker-compose
I tried Lazy Initialization with Spring Boot 2.2.0
Spring Boot Form
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Asynchronous processing with Spring Boot using @Async
Implement paging function with Spring Boot + Thymeleaf
Spring Boot Memorandum
gae + spring boot
Javaw.exe error when starting Spring Boot (STS)
(IntelliJ + gradle) Hello World with Spring Boot
Use cache with EhCashe 2.x with Spring Boot
Form class validation test with Spring Boot
Run WEB application with Spring Boot + Thymeleaf
Achieve BASIC authentication with Spring Boot + Spring Security
Spring Boot environment construction with Docker (January 2021 version)
Create a website with Spring Boot + Gradle (jdk1.8.x)
Configure Spring Boot application with maven multi module