[JAVA] Hello world! With Spring Boot (Marven + text editor)

Introduction

Try Hello world while looking at Original Documents It was.

I think that the original document will be updated in the future, so As a gyotaku, I will keep a record that the java, marven, spring versions are like this when done in the following state.

$ java -version
java version "11.0.7" 2020-04-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.7+8-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.7+8-LTS, mixed mode)
$ mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/Cellar/maven/3.6.3_1/libexec
Java version: 13.0.2, vendor: N/A, runtime: /usr/local/Cellar/openjdk/13.0.2+8_2/libexec/openjdk.jdk/Contents/Home
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.4", arch: "x86_64", family: "mac"
$ spring --version
Spring CLI v2.2.6.RELEASE

Create a project folder

The name is "spring-boot-hello" for the time being.

$ mkdir spring-boot-hello
$ cd spring-boot-hello

Create POM.xml

Create pom.xml directly under the spring-boot-hello folder. Create a file, open it with a text editor, and paste the following content.

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 https://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.2.6.RELEASE</version>
    </parent>

    <description/>
    <developers>
        <developer/>
    </developers>
    <licenses>
        <license/>
    </licenses>
    <scm>
        <url/>
    </scm>
    <url/>

    <!-- Additional lines to be added here... -->

</project>

This pom.xml Gemfile for RubyOnRails Package.json for JavaScript I think it's a file.

Add a dependency.

Add "spring-boot-starter-web" required for web service development to pom.xml.

Added the following after Addtional lines to be add here. (The original document was written under parent, but I thought it would be better, so I wrote it here without permission.)

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

Please be careful about nesting when pasting.

To check the dependency, hit the following command.

$ mvn dependency:tree

I think that the various functions required to run the web service are displayed as dependencies.

[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:myproject >------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.1.2:tree (default-cli) @ myproject ---
[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.2.6.RELEASE:compile
[INFO]    +- org.springframework.boot:spring-boot-starter:jar:2.2.6.RELEASE:compile
[INFO]    |  +- org.springframework.boot:spring-boot:jar:2.2.6.RELEASE:compile
[INFO]    |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.6.RELEASE:compile
[INFO]    |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.6.RELEASE:compile
[INFO]    |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO]    |  |  |  +- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO]    |  |  |  \- org.slf4j:slf4j-api:jar:1.7.30:compile
[INFO]    |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.12.1:compile
[INFO]    |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.12.1:compile
[INFO]    |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile
[INFO]    |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO]    |  +- org.springframework:spring-core:jar:5.2.5.RELEASE:compile
[INFO]    |  |  \- org.springframework:spring-jcl:jar:5.2.5.RELEASE:compile
[INFO]    |  \- org.yaml:snakeyaml:jar:1.25:runtime
[INFO]    +- org.springframework.boot:spring-boot-starter-json:jar:2.2.6.RELEASE:compile
[INFO]    |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.10.3:compile
[INFO]    |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.10.3:compile
[INFO]    |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.10.3:compile
[INFO]    |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.10.3:compile
[INFO]    |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.10.3:compile
[INFO]    |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.10.3:compile
[INFO]    +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.6.RELEASE:compile
[INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.33:compile
[INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.33:compile
[INFO]    |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.33:compile
[INFO]    +- org.springframework.boot:spring-boot-starter-validation:jar:2.2.6.RELEASE:compile
[INFO]    |  +- jakarta.validation:jakarta.validation-api:jar:2.0.2:compile
[INFO]    |  \- org.hibernate.validator:hibernate-validator:jar:6.0.18.Final:compile
[INFO]    |     +- org.jboss.logging:jboss-logging:jar:3.4.1.Final:compile
[INFO]    |     \- com.fasterxml:classmate:jar:1.5.1:compile
[INFO]    +- org.springframework:spring-web:jar:5.2.5.RELEASE:compile
[INFO]    |  \- org.springframework:spring-beans:jar:5.2.5.RELEASE:compile
[INFO]    \- org.springframework:spring-webmvc:jar:5.2.5.RELEASE:compile
[INFO]       +- org.springframework:spring-aop:jar:5.2.5.RELEASE:compile
[INFO]       +- org.springframework:spring-context:jar:5.2.5.RELEASE:compile
[INFO]       \- org.springframework:spring-expression:jar:5.2.5.RELEASE:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.657 s
[INFO] Finished at: 2020-04-28T14:40:57+09:00
[INFO] ------------------------------------------------------------------------

Write code

src/main/java/ Create a folder hierarchy called Example.java and create a file called Example.java under it.

Excample.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);
    }

}

Execute

$ mvn spring-boot:run

Various things are displayed on the terminal, but I think it will be a success if the following logo appears.

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

Check with browser

http://localhost:3000

When I accessed, it was displayed as Hello World.

spring-boot-hello.png

That is all. The source is on github.

Recommended Posts

Hello world! With Spring Boot (Marven + text editor)
Hello World with Spring Boot
Hello World with Spring Boot!
Hello World with Spring Boot
Until "Hello World" with Spring Boot
(Intellij) Hello World with Spring Boot
Hello World with Eclipse + Spring Boot + Maven
(IntelliJ + gradle) Hello World with Spring Boot
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Hello World (REST API) with Apache Camel + Spring Boot 2
Hello World (console app) with Apache Camel + Spring Boot 2
Spring Boot Hello World in Eclipse
[Practice! ] Display Hello World in Spring Boot
How Spring Security works with Hello World
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
Hello World at explosive speed with Spring Initializr! !! !!
Spring Boot2 Web application development with Visual Studio Code Hello World creation
Try to display hello world with spring + gradle
Hello World with Micronaut
Download with Spring Boot
Hello World comparison between Spark Framework and Spring Boot
Generate barcode with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Run LIFF with Spring Boot
SNS login with Spring Boot
Hello World with VS Code!
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
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
Hello World with SpringBoot / Gradle
Hello, World! With Asakusa Framework!
Create a Hello World web app with Spring framework + Jetty
Create an app with Spring Boot 2
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Inquiry application creation with Spring Boot
Hello world with Kotlin and JavaFX
Hello World with Docker and C
Get validation results with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
Hello World with GlassFish 5.1 + Servlet + JSP
Create PDF with itext7 ~ Hello World ~
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
"Hello world" for ImageJ with Eclipse
I tried Flyway with Spring Boot
Hello World with GWT 2.8.2 and Maven
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
Processing at application startup with Spring Boot
Spring5 MVC Web App Development with Visual Studio Code Hello World Creation