在使用Spring Boot(结合Maven和文本编辑器)时打印出Hello world!
首先
我在查阅本家的文档时尝试了Hello world。
因为我认为未来本公司的文档将会被更新,所以我会在下面的状态中记录下鱼拓,并留存关于Java、Marven和Spring版本的信息。
$ 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
创建项目文件夹
暂时将其命名为 “spring-boot-hello”。
$ 创建一个名为 spring-boot-hello 的文件夹
$ 进入 spring-boot-hello 文件夹
创建POM.xml文件
在spring-boot-hello文件夹的根目录下创建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>
我认为这个pom.xml文件相当于在RubyOnRails中的Gemfile文件,在JavaScript系列中的package.json文件。
添加依赖关系。
在pom.xml中添加必要的”spring-boot-starter-web”,以用于开发Web服务。
在这里添加附加行之后,请追加以下内容。
(原始文件中写着是在parent下面,但我认为在这里更好,所以我自作主张地写在这里。)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
在粘贴时,请注意嵌套等情况。
要确认依存关系,请执行下面的命令。
$ mvn dependency:tree
我认为所展示的是运行Web服务所需的各种功能之间的依赖关系。
[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] ------------------------------------------------------------------------
写代码 (xiě mǎ)
在src/main/java/目录下创建一个名为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);
}
}
执行
$ mvn spring-boot:run
当终端上显示各种信息时,如果出现以下标志,我认为就是成功的。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.6.RELEASE)
在浏览器中确认。
当我访问该网页时,显示出”Hello World”。
以上就是了。
源代码已放在GitHub上。