上一篇文章已經使用Maven 搭建了一個WEB項目。
現在需要搭建spring boot框架,並實現一個HelloWorld的項目,讓程序真正運行起來。
一、在pom.xml中引入spring-boot-start-parent,spring官方的叫stater poms,
它可以提供dependency management,也就是依賴管理,引入以後在聲明其它dependency的時候就不需要version了。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
二、需要在pom.xml中引入spring-boot-starter-web,spring官方解釋spring-boot-start-web包含了spring webmvc和tomcat等web開發的特性。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
三、如果我們要直接Main啓動spring,那麼以下plugin必須要添加,否則是無法啓動的。如果使用maven的spring-boot:run的話就不需要此配置。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
四、下面開始寫程序,我們需要一個啓動類,然後在啓動類聲明讓spring boot自動給我們配置spring需要的配置,比如:@SpringBootApplication,爲了可以儘快讓程序跑起來,我們簡單寫一個通過瀏覽器訪問hello world字樣的例子:
package com.whf.chen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author:辰
* @E-mail:[email protected]
* @CSDN博客:http://blog.csdn.net/chenfengbao
* @2018年2月22日下午4:18:21
*/
@SpringBootApplication
@RestController
public class SpringBootHelloWorld {
@RequestMapping("/chen")
public String hello() {
return "Hello Spring Boot!";
}
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorld.class, args);
}
}
其中@SpringBootApplication聲明讓spring boot自動給程序進行必要的配置,等價於以默認屬性使用@Configuration,@EnableAutoConfiguration和@ComponentScan。
@RestController返回json字符串的數據,直接可以編寫RESTFul的接口。
五、運行我們的Application,我們先介紹第一種運行方式。
第一種方式特別簡單:右鍵Run As -> Java Application。之後打開瀏覽器輸入地址:http://127.0.0.1:8080/chen 就可以看到Hello Spring Boot!了。
第二種方式右鍵project – Run as – Maven build – 在Goals裏輸入spring-boot:run ,然後Apply,最後點擊Run。
六、完整的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.whf.chen</groupId>
<artifactId>springboot_helloworld</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_helloworld Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>springboot_helloworld</finalName>
</build>
</project>
至些,我們的Spring boot項目已經搭建完成,併成功的運行了HelloWorld的項目。