springboot工程搭建(入門案例)
第一步:創建maven工程
第二步:設置項目信息
第三步:默認項目名稱,不用改動(第二步已填寫)
第三步:在pom.xml中導入依賴
SpringBoot要求,項目要繼承SpringBoot的起步依賴spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
SpringBoot要集成SpringMVC進行Controller的開發,所以項目要導入web的啟動依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
第四步:編寫
1 import org.springframework.boot.SpringApplication; 2 importorg.springframework.boot.autoconfigure.SpringBootApplication; 3 4 @SpringBootApplication 5 public class MySpringBootApplication { 6 7 public static void main(String[] args) { 8 SpringApplication.run(MySpringBootApplication.class); 9 } 10 11 }
1 import org.springframework.stereotype.Controller; 2 import org.springframework.web.bind.annotation.RequestMapping; 3 import org.springframework.web.bind.annotation.ResponseBody; 4 5 @Controller 6 public class StartController { 7 8 @RequestMapping("/hello") 9 @ResponseBody 10 public String init(){ 11 return "hi,springboot!"; 12 } 13 14 }
第六步:啟動MySpringBootApplication.java的主方法(即啟動項目)
項目啟動完成........開心
測試結果如下:
完成項目搭建!!!!