要求:搭建一个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