-
表单提交会带上_method=PUT
-
请求过来被HiddenHttpMethodFilter拦截
-
请求是否正常,并且是POST
-
获取到_method的值
-
兼容以下请求;PUT.DELETE.PATCH
-
原生request(post),包装模式requesWrapper重写了getMethod方法,返回的是传入的值。
-
过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的。
@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
@ConditionalOnProperty(prefix = “spring.mvc.hiddenmethod.filter”, name = “enabled”, matchIfMissing = false)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new OrderedHiddenHttpMethodFilter();
}
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
从这里我们可以知道只要我们自己配置了bean,那么就用我们自己的
@ConditionalOnProperty(prefix = “spring.mvc.hiddenmethod.filter”, name = “enabled”, matchIfMissing = false)
从这我们可以知道默认这个过滤器是不开启的,要开启的话需要我们手动开启
进入HiddenHttpMethodFilter类
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
HttpServletRequest requestToUse = request;
if (“POST”.equals(request.getMethod()) && request.getAttribute(“javax.servlet.error.exception”) == null) {
// 获取到_method的值
String paramValue = request.getParameter(this.methodParam);
if (StringUtils.hasLength(paramValue)) {
String method = paramValue.toUpperCase(Locale.ENGLISH);
// 兼容以下请求;PUT.DELETE.PATCH
if (ALLOWED_METHODS.contains(method)) {
//包装模式requesWrapper重写了getMethod方法,返回的是传入的值。
requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method);
}
}
}
//过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的。
filterChain.doFilter((ServletRequest)requestToUse, response);
}
1.3 rest使用客户端工具如postman 则无需使用filter
2.请求映射原理
先回顾下SpringMvc的原理图
SpringMVC功能分析都从 org.springframework.web.servlet.DispatcherServlet------> doDispatch()
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// 找到当前请求使用哪个Handler(Controller的方法)处理
mappedHandler = getHandler(processedRequest);
//HandlerMapping:处理器映射。/xxx->>xxxx
RequestMappingHandlerMapping:保存了所有@RequestMapping 和handler的映射规则。
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
if (this.handlerMappings != null) {
for (HandlerMapping mapping : this.handlerMappings) {
HandlerExecutionChain handler = mapping.getHandler(request);
if (handler != null) {
return handler;
}
}
}
return null;
}
-
SpringBoot自动配置欢迎页WelcomePageHandlerMapping 。访问 /能访问到index.html;
-
SpringBoot自动配置了默认 的 RequestMappingHandlerMapping
-
请求进来,挨个尝试所有的HandlerMapping看是否有请求信息。
-
如果有就找到这个请求对应的handler
如果没有就是下一个 HandlerMapping
- 我们需要一些自定义的映射处理,我们也可以自己给容器中放HandlerMapping。自定义 HandlerMapping
3.请求处理—常用的参数注解
@PathVariable、@RequestHeader、@ModelAttribute、@RequestParam、@MatrixVariable、@CookieValue、@RequestBody @RequestAttribute
@RestController
public class ParameterTestController {
// car/2/owner/zhangsan
@GetMapping(“/car/{id}/owner/{username}”)
public Map<String,Object> getCar(@PathVariable(“id”) Integer id,
@PathVariable(“username”) String name,
@PathVariable Map<String,String> pv,
@RequestHeader(“User-Agent”) String userAgent,
@RequestHeader Map<String,String> header,
@RequestParam(“age”) Integer age,
@RequestParam(“inters”) List inters,
@RequestParam Map<String,String> params,
@CookieValue(“_ga”) String _ga,
@CookieValue(“_ga”) Cookie cookie){
Map<String,Object> map = new HashMap<>();
// map.put(“id”,id);
// map.put(“name”,name);
// map.put(“pv”,pv);
// map.put(“userAgent”,userAgent);
// map.put(“headers”,header);
map.put(“age”,age);
map.put(“inters”,inters);
map.put(“params”,params);
map.put(“_ga”,_ga);
System.out.println(cookie.getName()+“===>”+cookie.getValue());
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
读者福利
更多笔记分享
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
y.csdnimg.cn/images/e5c14a7895254671a72faed303032d36.jpg" alt=“img” style=“zoom: 33%;” />
读者福利
[外链图片转存中…(img-WA4yjJuw-1713309136007)]
更多笔记分享
[外链图片转存中…(img-zBdkDf0S-1713309136007)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!