要求:搭建一個springboot項目並輸出helloworld;
開發環境:Idea2019
框架:Springboot
工具:Maven
新建springboot項目
這裏選擇的項目是springboot項目直接點next
查看項目結構,檢查依賴是否導入
pom文件
//依賴
<dependencies>
//web的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
//測試單元junit
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
// 打包工具
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
導入依賴成功用junit測試一下:
測試web:
- 創建controller層(這裏我們只測試controller層所以先不創建service和dao層)
注意:conntroller這個目錄必須和SpringbootHelloworldApplication必須在同一目錄下。
- 編寫測試代碼:
package com.feng.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
// 表明這個類是controller
@Controller
// 返回的類型是字符串
@ResponseBody
public class HelloController {
// 請求的地址爲 hello
@RequestMapping("/hello")
public String hello(){
// 返回字符串“ hello,world”
return "hello,world";
}
}
- 運行測試:
訪問127.0.0.1:8080/hello
返回hello,world
測試成功
在這裏對所有程序員和未來程序員說一句話:即使在小的帆也能遠航!
給大家推薦一些很好的視頻:
https://space.bilibili.com/95256449?spm_id_from=333.788.b_765f7570696e666f.1