<b>1.3.2 一個簡單的執行個體</b>
spring boot的官方文檔中提供了一個最簡單的web執行個體程式,這個執行個體隻使用了幾行代碼,如代碼清單1-3所示。雖然簡單,但實際上這已經可以算作是一個完整的web項目了。
代碼清單1-3 spring boot簡單執行個體
package
springboot.example;
import
org.springframework.boot.springapplication;
org.springframework.boot.autoconfigure.springbootapplication;
org.springframework.web.bind.annotation.requestmapping;
org.springframework.web.bind.annotation.restcontroller;
@springbootapplication
@restcontroller
public class
application {
@requestmapping("/")
string home() {
return "hello";
}
public static void main(string[] args) {
springapplication.run(application.class, args);
}
這個簡單執行個體,首先是一個spring boot應用的程式入口,或者叫作主程式,其中使用了一個注解@springbootapplication來标注它是一個spring boot應用,main方法使它成為一個主程式,将在應用啟動時首先被執行。其次,注解@restcontroller同時标注這個程式還是一個控制器,如果在浏覽器中通路應用的根目錄,它将調用home方法,并輸出字元串:hello。
1.4 運作與釋出
本章執行個體工程的完整代碼可以使用idea直接從github的https://github.com/chen-fromsz/spring-boot-hello.git中檢出,如圖1-15所示,單擊clone按鈕将整個項目複制到本地。
圖1-15 檢出執行個體工程