[JAVA] Erstellen Sie eine Spring Boot-Anwendung mit IntelliJ IDEA

Dies ist das Verfahren zum Erstellen und Ausführen einer Spring Boot-Anwendung mit IntelliJ IDEA. Bestätigen Sie, dass die Ausführung ausgeführt werden kann, indem Sie mit IntelliJ IDEA debuggen und eine JAR-Datei erstellen.

Entwicklungsumgebung

Vorbereitungen

Bereiten Sie Folgendes für die Entwicklung von Spring Boot vor.

Spring Boot-Entwicklung mit IntelliJ IDEA

Erstellen Sie ein Projekt gemäß dem Inhalt von Erste Schritte mit Spring Boot.

https://spring.pleiades.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started-first-application

Folgen Sie den unteren Schritten.

--Ein neues Projekt erstellen --Erstelle pom.xml

Erstellen Sie ein neues Projekt

Starten Sie IntelliJ IDEA und wählen Sie "Neues Projekt".

intelliJ_home.png

Wählen Sie Maven aus, geben Sie das im Project SDK installierte OpenJDK an und wählen Sie dann Weiter.

intelliJ_new_project_1.png

Geben Sie einen Namen für Ihr Projekt ein und wählen Sie Fertig stellen.

intelliJ_new_project_2.png

Bearbeiten Sie pom.xml

Beschreiben Sie den folgenden Inhalt in 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>org.example</groupId>
	<artifactId>spring-boot-demo-app</artifactId>
	<version>1.0-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.2.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.3.2.RELEASE</version>
			</plugin>
		</plugins>
	</build>
</project>

Laden Sie das Projekt nach dem Bearbeiten von pom.xml neu.

intelliJ_reload_module.png

Hallo Welt! Erstellen einer Anwendung

Erstellen Sie "src / main / java / Example.java".

intelliJ_new_file.png

Beschreiben Sie den folgenden Inhalt in Example.java.

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

	@RequestMapping("/")
	String home() {
		return "Hello World!";
	}

	public static void main(String[] args) {
		SpringApplication.run(Example.class, args);
	}

}

Führen Sie Programme mit IntelliJ IDEA aus

Lassen Sie uns das erstellte Programm ausführen. Drücken Sie die Run-Taste links neben der Hauptklasse, um das Programm auszuführen.

intelliJ_run.png

Bei Erfolg wird die folgende Meldung auf der Konsole angezeigt:

/Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=52512:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/masayuki/Documents/program/spring-boot-demo-app/target/classes:/Users/masayuki/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.3.2.RELEASE/spring-boot-starter-web-2.3.2.RELEASE.jar:/Users/masayuki/.m2/repository/org/springframework/boot/spring-boot-starter/2.3.2.RELEASE/spring-boot-starter-2.3.2.RELEASE.jar:/Users/masayuki/.m2/repository/org/springframework/boot/spring-boot/2.3.2.RELEASE/spring-boot-2.3.2.RELEASE.jar:/Users/masayuki/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.3.2.RELEASE/spring-boot-autoconfigure-2.3.2.RELEASE.jar:/Users/masayuki/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.3.2.RELEASE/spring-boot-starter-logging-2.3.2.RELEASE.jar:/Users/masayuki/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/masayuki/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/masayuki/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar:/Users/masayuki/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.13.3/log4j-to-slf4j-2.13.3.jar:/Users/masayuki/.m2/repository/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar:/Users/masayuki/.m2/repository/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar:/Users/masayuki/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/Users/masayuki/.m2/repository/org/springframework/spring-core/5.2.8.RELEASE/spring-core-5.2.8.RELEASE.jar:/Users/masayuki/.m2/repository/org/springframework/spring-jcl/5.2.8.RELEASE/spring-jcl-5.2.8.RELEASE.jar:/Users/masayuki/.m2/repository/org/yaml/snakeyaml/1.26/snakeyaml-1.26.jar:/Users/masayuki/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.3.2.RELEASE/spring-boot-starter-json-2.3.2.RELEASE.jar:/Users/masayuki/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.1/jackson-databind-2.11.1.jar:/Users/masayuki/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.1/jackson-annotations-2.11.1.jar:/Users/masayuki/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.1/jackson-core-2.11.1.jar:/Users/masayuki/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.11.1/jackson-datatype-jdk8-2.11.1.jar:/Users/masayuki/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.11.1/jackson-datatype-jsr310-2.11.1.jar:/Users/masayuki/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.11.1/jackson-module-parameter-names-2.11.1.jar:/Users/masayuki/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.3.2.RELEASE/spring-boot-starter-tomcat-2.3.2.RELEASE.jar:/Users/masayuki/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.37/tomcat-embed-core-9.0.37.jar:/Users/masayuki/.m2/repository/org/glassfish/jakarta.el/3.0.3/jakarta.el-3.0.3.jar:/Users/masayuki/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.37/tomcat-embed-websocket-9.0.37.jar:/Users/masayuki/.m2/repository/org/springframework/spring-web/5.2.8.RELEASE/spring-web-5.2.8.RELEASE.jar:/Users/masayuki/.m2/repository/org/springframework/spring-beans/5.2.8.RELEASE/spring-beans-5.2.8.RELEASE.jar:/Users/masayuki/.m2/repository/org/springframework/spring-webmvc/5.2.8.RELEASE/spring-webmvc-5.2.8.RELEASE.jar:/Users/masayuki/.m2/repository/org/springframework/spring-aop/5.2.8.RELEASE/spring-aop-5.2.8.RELEASE.jar:/Users/masayuki/.m2/repository/org/springframework/spring-context/5.2.8.RELEASE/spring-context-5.2.8.RELEASE.jar:/Users/masayuki/.m2/repository/org/springframework/spring-expression/5.2.8.RELEASE/spring-expression-5.2.8.RELEASE.jar Example

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2020-08-02 14:26:02.276  INFO 35892 --- [           main] Example                                  : Starting Example on maa with PID 35892 (/Users/masayuki/Documents/program/spring-boot-demo-app/target/classes started by masayuki in /Users/masayuki/Documents/program/spring-boot-demo-app)
2020-08-02 14:26:02.280  INFO 35892 --- [           main] Example                                  : No active profile set, falling back to default profiles: default
2020-08-02 14:26:04.567  INFO 35892 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-02 14:26:04.624  INFO 35892 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-02 14:26:04.625  INFO 35892 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-02 14:26:04.847  INFO 35892 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-02 14:26:04.848  INFO 35892 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2435 ms
2020-08-02 14:26:05.231  INFO 35892 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-02 14:26:05.676  INFO 35892 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-02 14:26:05.703  INFO 35892 --- [           main] Example                                  : Started Example in 4.953 seconds (JVM running for 6.18)

Wenn Sie mit einem Browser in diesem Status auf http: // localhost: 8080 zugreifen, wird Hello World! Angezeigt.

In JAR-Datei erstellen und ausführen

Klicken Sie in der Baumstruktur, die angezeigt wird, wenn Sie rechts auf "Maven" klicken, mit der rechten Maustaste auf "Projektname"> "Lebenszyklus"> "Paket" und wählen Sie "Maven Build ausführen".

intelliJ_build.png

Wenn der Build erfolgreich ist, wird die folgende Meldung auf der Konsole angezeigt.

/Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home/bin/java -Dmaven.multiModuleProjectDirectory=/Users/masayuki/Documents/program/spring-boot-demo-app "-Dmaven.home=/Applications/IntelliJ IDEA CE.app/Contents/plugins/maven/lib/maven3" "-Dclassworlds.conf=/Applications/IntelliJ IDEA CE.app/Contents/plugins/maven/lib/maven3/bin/m2.conf" "-Dmaven.ext.class.path=/Applications/IntelliJ IDEA CE.app/Contents/plugins/maven/lib/maven-event-listener.jar" "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=52530:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA CE.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds.license:/Applications/IntelliJ IDEA CE.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.6.0.jar" org.codehaus.classworlds.Launcher -Didea.version=2020.2 package
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.example:spring-boot-demo-app >------------------
[INFO] Building spring-boot-demo-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ spring-boot-demo-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ spring-boot-demo-app ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/masayuki/Documents/program/spring-boot-demo-app/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ spring-boot-demo-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/masayuki/Documents/program/spring-boot-demo-app/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ spring-boot-demo-app ---
[INFO] Changes detected - recompiling the module!
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ spring-boot-demo-app ---
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ spring-boot-demo-app ---
[INFO] Building jar: /Users/masayuki/Documents/program/spring-boot-demo-app/target/spring-boot-demo-app-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.3.2.RELEASE:repackage (repackage) @ spring-boot-demo-app ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.498 s
[INFO] Finished at: 2020-08-02T14:27:21+09:00
[INFO] ------------------------------------------------------------------------

Die Meldung auf der Konsole beim Erstellen zeigt, dass "[Projektstammordner] / target / [Projektname] -1.0-SNAPSHOT.jar" erstellt wurde. Suchen Sie daher nach der JAR-Datei und "java -jar [erstellt". Beginnen Sie mit der JAR-Datei] `. Bei Erfolg wird die folgende Meldung auf der Konsole angezeigt:

$ java -jar target/spring-boot-demo-app-1.0-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2020-08-02 14:27:49.758  INFO 35907 --- [           main] Example                                  : Starting Example on maa with PID 35907 (/Users/masayuki/Documents/program/spring-boot-demo-app/target/spring-boot-demo-app-1.0-SNAPSHOT.jar started by masayuki in /Users/masayuki/Documents/program/spring-boot-demo-app)
2020-08-02 14:27:49.765  INFO 35907 --- [           main] Example                                  : No active profile set, falling back to default profiles: default
2020-08-02 14:27:52.252  INFO 35907 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-02 14:27:52.306  INFO 35907 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-02 14:27:52.306  INFO 35907 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-02 14:27:52.528  INFO 35907 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-02 14:27:52.528  INFO 35907 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2622 ms
2020-08-02 14:27:53.043  INFO 35907 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-02 14:27:53.586  INFO 35907 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-02 14:27:53.613  INFO 35907 --- [           main] Example                                  : Started Example in 5.628 seconds (JVM running for 6.985)

Wenn Sie mit einem Browser in diesem Status auf http: // localhost: 8080 zugreifen, wird Hello World! Angezeigt.

Bisher haben wir die Ausführung und Erstellung der Spring Boot-Anwendung mit IntelliJ IDEA bestätigt.

Recommended Posts

Erstellen Sie eine Spring Boot-Anwendung mit IntelliJ IDEA
So erstellen Sie ein Spring Boot-Projekt in IntelliJ
Erstellen Sie mit IntelliJ ein Java Spring Boot-Projekt
Erstellen Sie mit Spring Boot eine einfache Such-App
Java-Anfänger haben versucht, mit Spring Boot eine einfache Webanwendung zu erstellen
Automatische Bereitstellung von in Java entwickelten Webanwendungen mit Jenkins [Spring Boot App Edition]
Spring Boot 2.3 Verfügbarkeit von Anwendungen
Stellen Sie die Spring Boot-Anwendung für Elastic Beanstalk bereit
Erstellen Sie einen Web-API-Server mit Spring Boot
Schritte zum Erstellen einer einfachen Kamel-App mit Apache Camel Spring Boot-Startern
Erstellen Sie mit Docker eine Spring Boot-Entwicklungsumgebung
Versuchen Sie, OpenID Connect mit Keycloak (Spring Boot-Anwendung) zu verwenden.
[JUnit 5-kompatibel] Schreiben Sie einen Test mit JUnit 5 mit Spring Boot 2.2, 2.3
[Spring Boot] So erstellen Sie ein Projekt (für Anfänger)
[Einführung in Spring Boot] Senden Sie ein Formular mit thymeleaf
Erstellen Sie mit Spring Security 2.1 eine einfache Demo-Site mit Spring Security
[Spring Boot] Erstellung von Webanwendungen
Erstellen Sie eine Lotterie mit Ruby
CICS-Java-Anwendung ausführen- (4) Spring Boot-App
Versuchen Sie es mit IntelliJ IDEA, da Sie dies nur einmal tun müssen
Versuchen Sie es mit Spring Boot Security
Erstellen Sie mit Spring Boot einen Mikrodienst
Bis Sie ein Spring Boot-Projekt in Intellij erstellen und an Github senden
Erstellen Sie mit IntelliJ IDEA eine Entwicklungsumgebung "Spring Thorough Introduction"
Ich habe mit Spring Boot ein einfaches MVC-Beispielsystem erstellt
[Android] Erstellen Sie einen Kalender mit GridView
Spring Boot-Anwendungsentwicklung in Eclipse
Überprüfungspunkte für den Spring Boot-Anwendungscode
Erstellen Sie mit Eclipse ein Jetty-Projekt
Vorbereiten der Erstellung einer Rails-Anwendung
Erstellen Sie ein Tomcat-Projekt mit Eclipse
Erstellen Sie eine App mit Spring Boot 2
Spring Boot Tutorial Verwenden der Spring Security-Authentifizierung
Erstellen Sie eine Anfrage-App mit Spring Boot
Erstellen Sie ein Java-Projekt mit Eclipse
(Intellij) Hallo Welt mit Spring Boot
Erstellen Sie eine App mit Spring Boot
Erstellen Sie eine Filterfunktion mit Acts-as-Taggable-On
Implementieren Sie die Spring Boot-Anwendung in Gradle
Ein Memo, das Spring Boot berührte
Erstellen wir eine Buchverwaltungs-Webanwendung mit Spring Boot part1
So erstellen Sie mit SPRING INITIALIZR einen Hinadan für ein Spring Boot-Projekt
Lassen Sie uns mit Spring Boot part3 eine Webanwendung für die Buchverwaltung erstellen
Lassen Sie uns mit Spring Boot part2 eine Webanwendung für die Buchverwaltung erstellen
Wird in Spring Boot mithilfe einer Bean-Definitionsdatei mit dem Namen application.xml angepasst
Erstellen Sie mit Docker schnell eine Webumgebung
Verarbeitung beim Starten einer Anwendung mit Spring Boot
Erstellen einer Spring Boot-Entwicklungsumgebung in Vagrant
Was ist eine Spring Boot-Originaldatei?
Erstellen Sie mit dem Befehl cURL + tar ein Spring Boot-App-Entwicklungsprojekt
Erstellen eines Projekts (und eines GitHub-Repositorys) mit Java und Gradle mit IntelliJ IDEA
Erstellen Sie mit Dropwizard eine einfache Webanwendung
Starten Sie mit IntelliJ ein (altes) Spring Boot-Projekt
Versuchen Sie es mit Spring Boot mit VS-Code
Starten Sie die Entwicklung von Webanwendungen mit Spring Boot
Starten Sie die Nginx + Spring Boot-Anwendung mit Docker-Compose
Erstellen Sie mit Grape einen RESTful-API-Service
Ich habe ein Plug-In für IntelliJ IDEA erstellt
Asynchrone Verarbeitung mit Spring Boot unter Verwendung von @Async
(IntelliJ + gradle) Hallo Welt mit Spring Boot