Pour les personnes qui démarrent Spring Boot à partir de zéro, j'écrirai un article dans le but de déplacer quelque chose si je le copie pour le moment. Basic Création d'un service Web RESTful est utilisé comme référence.
$ 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"
$
C'est un Mac dit normal, mais si vous avez Maven, vous pouvez l'exécuter sous Windows presque de la même manière.
Nous communiquerons également avec curl.
$ curl --version
curl 7.54.0 (x86_64-apple-darwin18.0) ...
$
Commençons par copier et coller pom.xml à partir de ce qui suit.
$ 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>
Créez un répertoire dans lequel vous placerez les fichiers source et de propriétés.
$ mkdir -p src/main/java
$ mkdir -p src/main/resources
Créez un fichier écrit par la magie qui démarre 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);
	}
}
Créez le contrôleur suivant qui décrit la fonction WEB.
$ 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;
    }
}
Créez un fichier de propriétés décrivant vos préférences.
$ 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 est le numéro de port à démarrer. Si la valeur par défaut 8080 est acceptable, aucune description n'est réellement requise. server.address est un sort pour se connecter à partir d'autres machines.
Commençons.
$ pwd
/xxxxx/xxx/myproject
$ mvn spring-boot:run
Diverses choses sont affichées,
...
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)
C'est bon!
Après le démarrage, essayez de communiquer à partir d'une autre invite.
$ curl http://localhost:8080/greeting
{"id":1,"content":"Hello, World!"}
$
Cela a si bien fonctionné!
Arrêtez le serveur WEB Spring Boot (Tomcat) que vous avez démarré avec Ctrl-C. Merci pour votre soutien.
$ mvn clean package
Vous disposez maintenant d'un fichier jar exécutable qui inclut Tomcat.
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar
Ce faisant, Tomcat démarrera de la même manière que le précédent `` mvn spring-boot: run ''. ..
$ mvn eclipse:clean eclipse:eclipse
Maintenant que vous disposez d'un fichier .project / .classpath, vous pouvez l'importer dans Eclipse.
Ajoutez les paramètres suivants à application.properties.
$ cat src/main/resources/application.properties
application.properties
...
spring.jackson.serialization.indent-output=true
Ctrl-C, redémarrez avec `` mvn spring-boot: run '', puis connectez-vous avec curl ...
$ curl http://localhost:8080/greeting
{
  "id" : 1,
  "content" : "Hello, World!"
}
Le JSON a été formaté. ..
Recommended Posts