Spring Boot的第一個HelloWorld例子

使用Spring Boot框架可以大大加速Web應用的開發過程。

一個最簡單的Web應用

  1. 新建一個maven項目

    這裏寫圖片描述

  2. 在pom文件中添加依賴
<!-- 添加spring boot父級依賴 -->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath />
    </parent>
<!-- 添加 web支持的starter pom -->
    <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>
            </plugin>
        </plugins>
    </build>

3.入口測試類

/**
 * 入口類
 * 
 * @author lizehao
 * @2018年1月15日下午3:10:37
 */
@SpringBootApplication
public class Application {

    /**
     * 作爲項目的入口
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

4.編寫控制器

@RestController
public class IndexController {
    @RequestMapping("/")
    public String index() {
        return "the first example of spring boot";
    }
}

5.直接運行main方法

這裏寫圖片描述

瀏覽器輸入: http://127.0.0.1:8080/

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章