首先創建一個maven項目,我選擇了war,剛創建pom文件頭報錯,鼠標移到錯誤提示那塊,點擊換成低版本的就行.
pom.xml中加入依賴:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.edu.xatu</groupId>
<artifactId>HelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<version>3.0</version>
</configuration>
</plugin>
</plugins>
</build>
</project>
創建一個Controller類:HelloSpringBoot.java:
package cn.edu.xatu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@SpringBootApplication
@Controller
public class HelloSpringBoot {
@RequestMapping("hello2")
@ResponseBody
public String hello(){
return "hello world hhhhhhhhh";
}
public static void main(String[] args) {
SpringApplication.run(HelloSpringBoot.class, args);
}
}
直接運行main方法,然後瀏覽器地址欄輸入:http://localhost:8080/hello2
這樣,一個簡單的Hello World已經創建完成了。
現在開始剛纔的操作進行分析:
(1)pom文件中繼承了spring-boot-starter-parent,引入基本的依賴管理配置。
(2)引入 spring-boot-starter-web,自動引入了SpringWeb相關的包
(3)註解 @SpringBootApplication。這個註解告訴SpringBoot自動的完成相關配置,包括基礎類的加載,bean的掃描等等,就是這個標籤爲我們的應用完成很多基本功能。
(4)SpringApplication.run(HelloSpringBoot.class, args); SpringBoot不再是一個web應用,需要我們自己去打包,部署,啓動tomcat , springboot默認把tomcat打包到應用中,我們可以以正常的運行jar的方式來運行springboot應用。