使用Spring Boot框架可以大大加速Web應用的開發過程。
一個最簡單的Web應用
新建一個maven項目
- 在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/