目录
一、springboot项目
(1)创建springboot项目
需要先下载好Spring Boot Helper插件。IDEA专业版不需要下载该插件。

进入IDEA创建新项目,选择Spring Initializr:

点击Next:

点击Next:

点击Next:

点击Create:

(2)目录介绍

(3)项目启动

这个是启动类,点击任意小绿箭头都可以运行项目。

项目启动成功,发现springboot项目里内置了tomcat
(4)运行一个程序

上面算是springmvc(后面解释)
运行启动类,在浏览器访问:

8080是因为web服务器是tomcat,默认端口号为8080,也可以修改端口号:

在这个里面写:server.port='端口号'
(5)通过其他方式创建和运行springboot项目
①创建项目其他方式
直接进入官网创建生成jar包,和IDEA创建方式是一样的

②运行jar包
java -jar jar包名字运行jar包
进入jar包所在命令页面,找到jar包

运行成功:

通过浏览器访问:

二、SpringMVC
1、MVC概念
是软件工程中的一种软件架构设计模式,他把软件系统分为三个模块:view(视图)、model(模型)、controller(控制器)。
view:与浏览器进行交互,获取请求数据以及接收响应;
model:主体部分,处理数据逻辑部分;
controller:可以理解为一个分发器,用来连接view和model,根据view发来的请求,决定使用那个model,以及决定将返回的响应分发给哪个视图。

2、SpringMVC概念
是一个实现了MVC模式的web框架。
3、SpringBoot与SpringMVC的联系与区别
Spring Boot是一个基于Spring框架的快速开发框架,它简化了Spring应用程序的搭建和配置过程,提供了一种快速开发的方式。而Spring MVC是Spring框架中的一个模块,用于构建Web应用程序,提供了一套MVC(Model-View-Controller)架构。
它们之间的联系在于,Spring Boot可以集成Spring MVC,通过Spring Boot可以更快速地构建和配置Spring MVC应用程序。Spring Boot提供了自动配置和起步依赖等功能,简化了Spring MVC应用程序的开发过程。Spring Boot是建立Spring MVC的一种方式。
它们的区别在于,Spring Boot是一个快速开发框架,提供了一种快速构建Spring应用程序的方式,而Spring MVC是一个Web框架,用于构建Web应用程序。Spring Boot可以集成Spring MVC,也可以集成其他的Spring模块,如Spring Data、Spring Security等,而Spring MVC主要用于构建Web应用程序。
上述在构建springboot时,就是选择了spring web,可以建立spring mvc项目,实现web开发。
4、创建springmvc项目
(1)建立连接
在springmvc中,是通过@RequestMappering注解,将浏览器与服务器之间建立连接。该注解后面跟访问的路径。注解在类上时,访问类中的方法时,路径=类路径+方法路径;类没有注解时,访问方法时,路径=方法路径。
eg:方法路径


类路径+方法路径:

不加类路径,会显示404.


@RestController注解的作用:将一个类标识为控制器,用于处理HTTP请求并返回响应。没有该注解时,有http请求时就无法访问该类。
(2)处理请求
①http请求:包括首行(方法、url、版本号)、请求头(header)、正文(get方法大概率没有)
②获取url中,查询字符串(对url的补充)的参数。
一个参数获取:



指定请求方式:
springmvc获取请求时,请求方式可以是get,也可以是post,也可以指定请求方式。
指定请求方式:

发送get请求与响应:

发送post请求与响应:

405表示方法不被允许。
两个参数获取:

age写为Integer是因为Integer可以接收null,int类型时,没有age会报错。
postman发送请求:

收到的响应:

参数重命名:

postman发送请求:

收到的响应:

但以上代码有个问题,username必须是必传的参数。

username默认是必传的参数,可进行修改:

请求与响应:

对象获取:
定义一个对象user:

获取对象:

请求与响应:

数组获取:

请求与响应:

或者:

集合获取:

请求与响应:

发现报错,500是服务器的错误,当参数是集合时,需要通过@RequestParam绑定参数关系。

请求与响应:

json正文获取:

请求与响应:

url参数获取:

请求与响应:

url中参数重命名获取:


上传文件获取:

请求与响应:

Cookie和Session:
概念见文章:cookie和session概念
Cookie获取:
①获取全部cookie:


②获取指定cookie:


session设置:


session获取:
①


②


③


请求header获取:
①


②


(3)返回响应
返回静态页面:
制作一个静态页面:index.html



发现返回的不是index.html页面,而是一串文字。当把代码做以下修改:


只是改了一个注解,为什么结果就不一样了???
之前说@RestController注解是将将一个类标识为控制器,用于处理HTTP请求并返回响应。那和@Controller注解的区别在哪里???
@RestController注解的源码:

发现是包括@Controller注解的
@Controller注解的源码:

与@RestController注解源码比较,发现少了@ResponseBody注解。
即:@Controller+@ResponseBody=@RestController
@Target注解:表示注解的使用范围;
@Retention注解:表示注解的生命周期;
@Controller注解:告诉Spring哪些类是控制器,需要被管理。响应默认返回的是页面;
@RestController注解:设定响应返回的是数据。
@Controller+@RestController,即:@RestController,使得响应返回的是数据。
所以在上面使用@RestController注解的时候返回的是一串字符串,但是将注解改为:@Controller时,返回的就是指定页面了。
返回HTML片段:


返回json:
自动确定返回类型:
Spring会根据返回值的类型自动确定返回类型,可以是JSON、XML等格式。
常见类型:
text/html : HTML格式
text/plain :纯文本格式
text/xml : XML格式
image/gif :gif图片格式
image/jpeg :jpg图片格式
image/png:png图片格式
application/xml :XML数据格式
application/json:JSON数据格式
application/pdf :pdf格式
eg:

刚刚返回的静态页面的响应,类型是html。


该响应类型为json
使用produces属性指定返回类型:

以上响应未指定类型时,返回的是纯文本类型(text/plain)。
指定类型为json:


设置响应状态码:


自定义响应header:

