Springboot
热部署
热部署:spring-boot-devtools
功能:修改文件并保存(ctrl+f9快捷保存)后自动帮你重新编译、启动项目。
配置:百度可多教程。
控制器
控制器:Controller
,在MVC模式中作为中转站:1.接收Model响应的数据,并返回给View
2.接收HTTP请求,并向Model请求信息
Springboot
提供了两种控制器:
@Controller
如果需要返回页面和数据,使用这个注解。前后端不分离开发时使用。通常与Thymeleaf模板引擎配合使用。@RestController
如果只需要返回数据,使用这个注解。默认情况下,此注解会将返回的对象数据转化为JSON
格式。
路由映射
@RequestMapping()
:主要负责URL的路由映射。可以放在类上,也可以放在方法上。
常用参数属性如下:value
指定url路径,默认,必须有。支持通配符(一个 "*" 匹配任意字符,两个 "**" 匹配任意路径);
method
指定请求方法。可以用 @GetMapping
等注解替代;
consumes
指定请求的媒体类型(Content-Type),如:application/json
;
produces
指定响应的媒体类型;
params,headers
指定请求的参数及请求头的值。
@RequestMapping("/hello")
public String hello(){
return "hello";
}
// http://localhost:8080/hello/xx/xx/xx 任意路径匹配
@RequestMapping("/hello/**")
public String hello(){
return "hello";
}
@RequestMapping(value = "/hello",method = RequestMethod.GET )
// 和 @GetMapping("/hello") 效果相同
public String hello(){
return "hello";
}
参数传递
接收参数的变量写在方法的参数里,需要确保两者名称相同。
// http://localhost:8080/hello?username=haha&phone=123
@GetMapping("/hello")
public String hello(String username,String phone){
return "hello "+username+" "+phone;
}
// Post可以接收Get参数,即拼接在url中的参数
// 即 http://localhost:8080/hello?username=haha&password=123 ,也可以被post接收
//需要保证类中属性名的名称 和 前台传过来参数名相同
@PostMapping("/hello")
public String hello(User user){
System.out.println(user);
return "POST请求";
}
@RequestParam()
:用于参数映射。即当用户传过来的参数名和用于接收的参数名不一样时,做一一对应用。并且在使用之后,默认里面的参数是必须的。如果没有对应参数,则下面的方法不会被访问到,直接返回错误界面。想让参数可选,需加上 required=False
进行调整。
@GetMapping("/hello")
public String hello(@RequestParam(value = "nickname",required = False) String username){
return "hello "+username;
}
@RequestBody()
:接收 JSON
格式数据。
// 除了名称保持一致之外,类型也要保持一致。
@PostMapping("/hello")
public String hello(@RequestBody User user){
System.out.println(user);
return "POST请求,Json格式";
}
@PathVariable
:获取动态路径上的参数
@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
public String delete(@PathVariable int id);
文件上传
注意表单类型。默认是 enctype="application/x-www-form-urlencoded"
,需要修改为 enctype="multipart/form-data"
在请求体中传递。几个数据之间有分隔符。
需要在配置文件中加入两个配置:1.限制单个文件上传大小的 spring.servlet.multipart.max-file-size = 10MB
2.限制单次请求大小的spring.servlet.multipart.max-request-size = 10MB
使用 MultipartFile
获取上传的文件数据,使用HttpServletRequest
获取路径
机器本地存储动态路径获取:String path = request.getServletContext().getRealPath("/upload/");
public String upload(String nickname, MultipartFile photo, HttpServletRequest request) throws IOException{
// 动态获取本机存储路径
String path = request.getServletContext().getRealPath("/upload/");
System.out.println(path);
// 调用saveFile 保存文件
saveFile(photo,path);
return "上传成功";
}
通过 transferTo
方法写入磁盘
public void saveFile(MultipartFile photo,String path) throws IOException{
// 判断upload文件夹是否存在,如果不存在,就创建。
File dir = new File(path);
if(!dir.exists()){
dir.mkdir();
}
// transferTo方法写入
File file = new File(path+photo.getOriginalFilename());
photo.transferTo(file);
}
拦截器
有三种方法:preHandle、postHandle、afterCompletion
。preHandle
用得最多。
可以通过重写方法来自定义拦截器。重写完之后需要在类中更新配置。
具体用到再搜吧qa。
RESTful接口设计风格
RESTful API 设计风格
Swagger 动态生成接口文档/进行调试
在Springboot
中加入对应依赖。注意Springboot2.6.x
后与Swagger
有版本冲突问题,需要另加配置。
Swagger
还提供了一些注解。
没有配置,用到的时候再搜吧QA
ApiPost 接口测试工具
直接下下来用就行。国产工具,都是中文很简单。
常见报错
1. 40x
405:方法不被允许
404:路径错误
400:传递参数错误
2. 50x
500:后端程序出现问题