Javaweb-SpringBoot框架
SpringBoot框架
静态资源:服务器上存储的不会改变的数据,通常不会根据用户的请求而变化。比如:HTML、CSS、JS、图片、视频等(负责页面展示)
服务器端根据用户请求和其他数据动态生成的,内容可能会在每次请求时都发生变化。比如:springboot等(负责逻辑处理)
Servlet、JSP等处理方式早已过期,目前都是springboot
一、SpringBoot入门
1.Spring
- spring包含许多框架
- springboot是基于springframework搭建的
- springboot可以帮助我们快速构建应用程序
2.具体搭建
1
2
3
4
5
6
7
8
9
10
11
12
131.创建工程
2.勾选Spring Web依赖
3.定义方法类,并添加注解
4.src/main/java/com/itheima/JavaWebApplication.java下是启动类,用来启动整个项目
具体一个实现代码
@RestController //表明当前是一个请求处理类
public class HelloController {
@RequestMapping("/hello")//表示该方法处理/hello请求
public String HelloWorld(String name){
return "Hello"+name+"!";
}
}重要的文件以及目录
src //代码主目录 pom.xml //maven配置问价1
2
3
4
5
6
7
8
* **遇到问题**
* **源根之外**
* ~~~shell
1.我是直接删除项目重现创建项目实现
2.也有可能是应为项目没有构建完成,耐心等待即可官方网站打不开
使用阿里云 start.aliyun.com1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
### 3.入门程序详解
* **在创建官方项目时,已经引入了起步依赖**
* 
## 二、http协议
### 1.定义
* **概念:Hyper Text Transfer Protocol,超文本传输协议,规定了浏览器和服务器之间数据传输的规则**
* **具体请求数据**
* ~~~shell
GET /hello?name=chuan HTTP/1.1 //请求行
//请求头
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
Cache-Control: max-age=0
Connection: keep-alive
Host: 127.0.0.1:8080
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0
sec-ch-ua: "Microsoft Edge";v="141", "Not?A_Brand";v="8", "Chromium";v="141"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
响应
HTTP/1.1 200 //响应行 //响应头 Content-Type: text/html;charset=UTF-8 Content-Length: 11 Date: Thu, 23 Oct 2025 07:22:08 GMT Keep-Alive: timeout=60 Connection: keep-alive1
2
3
4
5
6
7
8
9
10
* **优势**
* ~~~shell
基于TCP协议:面向连接,安全
基于请求-响应模型的:一次请求对应一次响应
HTTP协议是无状态的协议:对于事务处理没有记忆能力。每次请求-响应都是独立的。
缺点:多次请求间不能共享数据。
优点:速度快
2.具体剖析
请求头具体代表内容
Host 请求的主机名 User-Agent 浏览器版本,例如Chrome浏览器的标识类似Mozilla/5.0 … Chrome/79,IE浏览器的标识类似Mozilla/5.0 (Windows NT …) like Gecko Accept *表示浏览器能接收的资源类型,如text/,image/或者/表示所有;* AcceptLanguage 表示浏览器偏好的语言,服务器可以据此返回不同语言的网页; AcceptEncoding 表示浏览器可以支持的压缩类型,例如gzip, deflate等。 Content-Type 请求主体的数据类型。 Content-Length 请求主体的大小(单位:字节)。 请求体
- 距离请求头需要空一行
3.服务器获取请求数据
tomcat服务器在获取对应请求数据时,已经将获取数据包包装到HttpSeveletRequest类,运行代码时直接声明就好
具体代码
@RestController //表明当前是一个请求处理类 public class HelloController { @RequestMapping("/hello")//表示该方法处理/hello请求 //已经保存道HttpServletRequest类对象中 public String HelloWorld(HttpServletRequest request ){ }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
* ~~~java
@RestController //表明当前是一个请求处理类
public class HelloController {
@RequestMapping("/hello")//表示该方法处理/hello请求
public String HelloWorld(HttpServletRequest request //直接定义HttpServletRequest对象就ok){
//获取请求方式
String method =request.getMethod();
System.out.println("请求方式:"+method);
//获取请求路径
String path = request.getServletPath();
System.out.println("请求路径:"+path);
//获取请求url以及uri
String url = request.getRequestURL().toString();
String uri = request.getRequestURI();
System.out.println("请求url:"+url);
System.out.println("请求uri:"+uri);
//获取请求协议
String protocol = request.getProtocol();
System.out.println("请求协议:"+protocol);
//获取请求参数
String name = request.getParameter("name");
System.out.println("请求参数:"+name);
//获取请求头
String header = request.getHeader("User-Agent");
System.out.println("请求头:"+header);
return "Hello World!";
}
}
4.响应数据

响应状态码
一共有五大类状态码
重要的为
200 客户端请求成功。 404 请求资源不存在,URL输入有误,或者网站资源被删除了。 500 服务器发生不可预期的错误。
具体请求头含义
5.服务器操作响应数据
已经包装到HttpServletResponse类对象中
一共有俩种方式
利用HttpServletResponse类对象
@RestController public class Response { @RequestMapping("/response") public void testResponse(HttpServletResponse response) throws IOException { //设置状态码 response.setStatus(HttpServletResponse.SC_OK); //设置响应头 response.setHeader("Content-Type","text/html;charset=utf-8"); //设置响应体 response.getWriter().write("<h1>Hello World!</h1>"); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
* **利用ResponseEntity泛型声明**
* ~~~java
@RestController
public class Response {
@RequestMapping("/response")
public ResponseEntity <String> testResponse() {
//设置状态码
return ResponseEntity
.status(200)
.header("name","itheima")
.body("hello world");
}
}
三、Springboot案例
resources/static目录下放置js,html等等静态页面
1
2
3
4InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
通过这个方式获取user.txt更加完美一些
直接获取resorce目录下文件- 1️⃣
this.getClass()
2️⃣.getClassLoader()
3️⃣.getResourceAsStream("user.txt")
- 1️⃣
开发者工具Fetch/XHR可以直接过滤axios请求
1
2
3
4
5
6
7return users;
最后返回一个list可以直接当作json数据返回
底层有一个
会将Controller返回的数据直接返回,如果是对象/集合会转换为json数据1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19//具体代码
public class UserController {
public List<User> list(){
InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
ArrayList<String> lines=IoUtil.readLines(in, "UTF-8", new ArrayList<>());
List<User> users=lines.stream().map(line ->{
String[] parts = line.split(",");
LocalDateTime updatetime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
User user = new User(Integer.parseInt(parts[0]),parts[1],parts[2],parts[3],Integer.parseInt(parts[4]),updatetime);
return user;
}).collect(Collectors.toList());
return users;
}
}
四、分层解耦-三层架构
1.三层架构

本质上就是将他们分层
1
2
31.controller层 接收前端请求处理数据
2.sercice层 业务逻辑处理,处理具体的业务逻辑
3.Dao层 负责数据增删改查
2.聚合解耦
- 耦合:各个类之间的依赖关联程度
- 聚合:软件中各个功能模块内部的功能练习
- 原则:高内聚低耦合
- 例如:
3.分层解耦
控制反转-容器实现 IOC
@Component 注解,表示交给ioc容器管理1
2
3
4
5
6
7
* **依赖注入-排队等待 DI**
* ~~~java
@Autowired
注解,表示自动进行依赖注入
会自动查询给类型bean对象,并赋值
bean对象,被管理的对象

4.IOC详解
ioc详细

三层架构分别对应
- 他们底层原本就封装了一层component注解
可以通过监控面板详细查看到有多少bean
Actoaur

声明注解的时候可以直接为value属性指定bean名字
@Component("userD ao")1
2
3
4
5
6
7
* **扫描**
* **声明bean的四类注解要想生效,需要扫描**
* ~~~java
@ComponentScanjava中启动类的注解已经包含
但是范围只是启动类所在包以及子包
5.DI详解
基于@Autowired注解进行依赖注入一共实现由三种方式
属性注入
public class Serviceimpl implements userservice { @Autowired public userdaoimpl userdao ;1
2
3
4
5
6
7
8
9
10
11
* **会破坏封装性,但是最优解**
* **构造函数注入**
* ~~~java
public final userdaoimpl userdao ;
@Autowired
public Serviceimpl(userdaoimpl userdao) {
this.userdao = userdao;
}如果只有一个构造函数,那@Autowired可以省略
.setter方法注入
@Autowired public void setUserdao(userdaoimpl userdao) { this.userdao = userdao; }保持类的封装性
默认是按照类型注解
- 如果注入时有多个类,会报错
- 解决方案
- @Primary
- @Qualifier
- @Resource
- 他是按照名称注入,Autowired时按照类型注入

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 十七.!