Ant's Blog

SpringBoot快速HelloWorld教程

注意事项

使用Spring Initializr可以快速生成一个Spring Boot工程。Spring Initializr既可以在网页端使用,也可以在IDEA Ultimate版本中使用。(Community版本不行;使用IDEA里的Spring Initializr时需要连接网络)

参考链接Tutorial: Create your first Spring application | IntelliJ IDEA Documentation (jetbrains.com)

需要注意的是,此方法在New Project窗口时,需要把默认的Gradle修改为Maven。
同时,Java版本应该是17或者20,。Java8现已不受支持。
本教程使用IDEA Ultimate 2023.1,Oracle OpenJDK20,Java17,Spring Boot3.0.6.

详细步骤

新建工程

在IDEA中点击File – New – Project,在左边边栏中选择Spring Initializr。
之后,将项目名称命名为spring-boot-tutorial(如果使用Jetbrains教程中的代码,建议按这个名称走,后面就不用改了)
Type选择Maven。(IDEA2023.1默认是Gradle)
Java版本选择17或者20。
点击Next,添加依赖。按照教程,选择Web – Spring Web。
点击Create。

运行HelloWorld

src/main/java/com/example/springboottutorial/SpringBootTutorialApplication.java文件中写入以下代码

package com.example.springboottutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringBootTutorialApplication {

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

    @GetMapping("/hello")
    public String sayHello(@RequestParam(value = "myName", defaultValue = "World") String name) {
        return String.format("Hello %s!", name);
    }

}

之后,在IDEA中运行项目,然后访问 http://localhost:8080/hello 即可看到Hello World。
如果IDEA中不能直接通过“运行”(绿色三角)按钮来无痛的运行项目,说明配置出了一些问题。

What's more

The created Spring Boot application has one endpoint available at /hello. However, if you open the root context of your application at http://localhost:8080/, you will get an error because there is no root resource defined. Let's add a static HTML home page with links to your endpoint.

1.Create the index.html file under /src/main/resources/static/.

In the Project tool window, right-click the /src/main/resources/static/ directory, select New | HTML File, specify the name index.html, and press Enter.

2.Modify the default template or replace it with the following HTML code:

```html
<!DOCTYPE HTML>
<html>
    <head>
        <title>Your first Spring application</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <p><a href="/hello">Greet the world!</a></p>

        <form action="/hello" method="GET" id="nameForm">
            <div>
                <label for="nameField">How should the app call you?</label>
                <input name="myName" id="nameField">
                <button>Greet me!</button>
            </div>
        </form>
    </body>
</html>


3.In the Run tool window, click The Rerun button or press Shift+F10 to restart your Spring application.

Now your application will serve index.html as the root resource at http://localhost:8080/

2 评论

  1. Thanks a lot for your sharing!!!

发表回复