姓名:陳中嬌 班級:軟件151
第一步:在Eclipse下面配置Maven環境:
一、使用spring boot新建maven工程不在需要建立maven web工程,只要一般的maven工程就好了。
二、maven包的導入
清單如下:
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.BUILD-SNAPSHOT</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency
<!-- spring data jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
以上為部分spring boot 構建簡單web項目所需要的maven配置文件。
第二步:構建Maven+spring web 項目 :
1.打開Eclipse,選擇新建Maven Project
2.然后利用向導分別建立一個webapp項目和quickStart項目
在新建的過程中會要設置要設置幾個東西groupId = cn.springbooot artifactId =SpringBootFrist ,剩下的設置就用默認的就可以了。
3.然后是將 webapp項目下面的WebApp目錄復制到quickstart項目之中,最后在在SpringBootFirst工程下面新建一個src/main/resources 目錄來配合Maven的目錄結構。這樣最后形成的SpringBootFirst工程就已經基本實現了整體的框架。
Spring boot 實現簡單的RestFul項目
第一步: pom.xml 配置文件的設置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
上面這個是實現Spring Boot中web服務最基本的配置,寫在pom.xml中就可以了。
第二步:編寫Java代碼
首先我將Spring Boot官方所給的代碼例子貼在下面,以此說明,在Spring Boot的項目運行,部署和發布,我們需要的東西不是很多。
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
} }
將上述的代碼放入SpringBootFirst工程的src/main/java目錄下面,進行運行,再在瀏覽器中輸入http://localhost:8080/ ,我們就能看到“Hello,World”了。