使用Spring Boot编写Hello World程序

环境

    • Windows 10, コマンド実行はコマンドプロンプトから

 

    • Maven 3.6.0

 

    Java 11

使用Maven创建一个空项目。

使用maven-archetype-quickstart指定archetype,执行mvn archetype:generate命令以生成项目。

> mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DgroupId=net.hidakanoko -DartifactId=spring-boot-hello-world -Dversion=1.0.0-SNAPSHOT
> cd spring-boot-hello-world

POM的编辑

在prent中指定spring-boot-starter-parent。

(...省略)
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

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

  <dependencies>
(...省略)

如果使用”spring-boot-starter-parent”作为父项目,可以很方便地进行各种配置,并且提供了dependency-management部分,可以省略依赖项的版本声明。

这个parent pom在Github上可以找到。
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/pom.xml

我们先确认一下此时mvn clean install成功。

>mvn clean install

(...省略)

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.193 s
[INFO] Finished at: 2019-03-17T10:38:22+09:00
[INFO] ------------------------------------------------------------------------

新增依赖库

Spring Boot Starter Web提供了构建Web服务所需的库依赖,包括RESTful API、MVC和嵌入式Tomcat等。

在dependencies中添加以下配置。

  <dependencies>

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

通过命令行执行mvn dependency:tree命令可以查看添加的依赖库。

编写代码

打开已经部署的App.java文件并进行以下编辑。

package net.hidakanoko;

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

@RestController
@EnableAutoConfiguration
public class App 
{
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

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

标注和处理

@RestController ステレオタイプと呼ばれるアノテーションで、コードを読む人とSpringに対してこのクラスが特定の役割を果たすことを示します。このアノテーションはクラスがWeb@Controllerであることを明示して、SpringがWebリクエストを適切に扱ってくれます。

@RequestMapping リクエストのルーティング情報を定義しています。指定されたパス “/” へのリクエストはhomeメソッドで処理されます。@RestControllerアノテーションが指定されている場合、メソッドの戻り値はそのまま呼び出し元に返されます。

@EnableAutoConfiguration このアノテーションを追加しておくと、jarの依存性等に応じてSpringをいい感じに設定してくれます。spring-boot-starter-webがSpring MVCやTomcatへの依存性を追加するので、このプロジェクトはWebアプリケーションであると判定され、Springもそのように設定されます。

mainメソッドはJavaのエントリーポイントです。ここではSpringApplication.runを呼び出しています。runメソッドにはプライマリソースとしてロードするクラスApp.classとコマンドラインパラーメーターがそのまま渡されています。

执行

使用命令行运行mvn spring-boot:run以启动应用程序。
当确认可以看到以下输出时,尝试在浏览器中访问http://localhost:8080。如果返回”Hello World”,则表示成功。

> mvn spring-boot:run

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

以下是一些建议的中文翻译选项:

1. https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/
请参考官方文档,网址为https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/。

2. https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/pom.xml
请访问该链接,其中包含Spring Boot的GitHub源代码信息,路径为https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/pom.xml。

3. https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
请到Maven仓库查找“org.springframework.boot”包的“spring-boot-starter-web”组件,网址为https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web。

bannerAds