1 HttpServletRequest 包结构更改

从 Spring6、Spring Boot3 开始 javax 包名称,修改为 jakarta。

  • 原来:javax.servlet.http.HttpServletRequest;
  • 修改后:jakarta.servlet.http.HttpServletRequest;

2 给项目加 favicon

什么是 favicon.ico :favicon.ico 是网站的缩略标志,可以显示在浏览器标签、地址栏左边和收藏夹,是展示网站个性的 logo 标志。

500

我们自己的网站定制 logo。首先找一个在线工具创建 favicon.ico。比如 https://quanxin.org/favicon ,用文字,图片生成我们需要的内容。生成的 logo 文件名称是 favicon.ico

step1:将生成的 favicon.ico 拷贝项目的 resources/ 或 resources/static/ 目录。

step2:在你的视图文件,加入对 favicon.ico 的引用。

在视图的 <head> 部分加入

<link rel="icon" href="../favicon.ico" type="image/x-icon" />

代码如下

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>视图</title>
    <link rel="icon" href="../favicon.ico" type="image/x-icon" />
  </head>
  <body></body>
</html>

2.1 5.2.1.1  匹配请求路径到控制器方法

SpringMVC 支持多种策略,匹配请求路径到控制器方法。AntPathMatcher 、 PathPatternParser

从 SpringBoot3 推荐使用 PathPatternParser 策略。比之前 AntPathMatcher 提示 6-8 倍吞吐量。

我们看一下 PathPatternParser 中有关 uri 的定义

通配符:

  • ? : 一个字符
  • * :0 或多个字符。在一个路径段中匹配字符
  • **:匹配 0 个或多个路径段,相当于是所有
  • 正则表达式:支持正则表达式

RESTFul 的支持路径变量

  • {变量名}
  • {myname:[a-z]+}: 正则匹配 a-z 的多个字面
  • {*myname}: 匹配多个路径一直到 uri 的结尾

例子:

// @GetMapping("/order/{*id}")
// {*id} 匹配 /order开始的所有请求, id表示order后面直到路径末尾的所有内容。
//       id自定义路径变量名称。与@PathVariable一样使用
// ( GET http://localhost:8080/order/1001
// ( GET http://localhost:8080/order/1001/2023-02-16
 
@GetMapping("/order/{*id}")
public String path4(@PathVariable("id") String orderId, HttpServletRequest request){
  return request.getRequestURI() + ",id="+orderId;
}
// 注意:@GetMapping("/order/{*id}/{*date}")无效的, {*..}后面不能在有匹配规则了
// @GetMapping("/pages/{fname:\\w+}.log")
// :\\w+ 正则匹配, xxx.log
// √ GET http://localhost:8080/pages/req.log
// √ GET http://localhost:8080/pages/111.log
// ✕ GET http://localhost:8080/pages/req.txt
// ✕ GET http://localhost:8080/pages/###.log
 
@GetMapping("/pages/{fname:\\w+}.log")
public String path5(@PathVariable("fname") String filename, HttpServletRequest request){
  return request.getRequestURI() + ",filename="+filename;
}
 

参数类型

类型作用
WebRequest, NativeWebRequest访问请求参数,作用同 ServletRequest,
jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponseServlet API 中的请求,应答
jakarta.servlet.http.HttpSessionServlet API 的会话
jakarta.servlet.http.PushBuilderServlet 4.0 规范中推送对象
HttpMethod请求方式
java.io.InputStream, java.io.ReaderIO 中输入,读取 request body
java.io.OutputStream, java.io.WriterIO 中输入,访问 response body
@PathVariable,@MatrixVariable,@RequestParam,@RequestHeader,@CookieValue,@RequestBody,@RequestPart,@ModelAttributeuri 模板中的变量,访问矩阵,访问单个请求参数,访问请求 header,访问 cookie,读取请求 body,文件上传,访问 model 中属性
Errors, BindingResult错误和绑定结果
java.util.Map, org.springframework.ui.Model, org.springframework.ui.ModelMap存储数据 Map,Model,ModelMap
其他参数String name, Integer , 自定义对象
完整 https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-arguments

返回值类型

返回值类型作用
@ResponseBody将 response body 属性序列化输出
HttpEntity<\B>, ResponseEntity<\B>包含 http 状态码和数据的实体
String实体名称或字符串数据
自定义对象如果有 json 库,序列化为 json 字符串
ErrorResponse错误对象
ProblemDetailRFC7807,规范的错误应答对象
void无返回值
ModelAndView数据和视图
java.util.Map, org.springframework.ui.Model作为模型的数据
https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-return-types