添加了Spring Boot基礎依賴後,如要使用web mvc功能,只須要添加以下啓動器便可,Spring Boot會自動裝配web功能。java
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
而後添加啓動類,建議放在根包下,這裏把Controller和啓動類放在一塊兒也是沒問題的,實際請分包出來。web
啓動類必需要有main方法,並添加啓動方法。spring
package hello; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @Controller @SpringBootApplication public class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } }
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> <fork>true</fork> <excludeDevtools>true</excludeDevtools> </configuration> </execution> </executions> </plugin> </plugins> </build>
Spring Boot默認的端口是8080,能夠經過server.port=8081來修改,或者經過命令行指定也行。微信
啓動工程後,訪問localhost:8080/就能輸出:Hello World!mvc
上面是經過maven手動建立工程的方式添加Spring Boot應用 ,固然新手也能夠打開Spring Boot的快速構建網站http://start.spring.io/,來選擇對應的啓動器生成項目,最後導入進來便可。app
推薦:Spring Boot & Cloud 最強技術教程maven
掃描關注咱們的微信公衆號,乾貨天天更新。spring-boot