当前位置: 首页 > news >正文

springBoot对REST支持源码解析

一、在配置类中:

@AutoConfiguration(after = { DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,ValidationAutoConfiguration.class })
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
public class WebMvcAutoConfiguration

提供了一个Filter:

	@Bean@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)@ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled")public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {return new OrderedHiddenHttpMethodFilter();}

该Fileter通过条件注解 检查配置文件是否开启参数处理:

spring:mvc:hiddenmethod:filter:enabled: true

提供一个默认参数名称:

	/** Default method parameter: {@code _method}. */public static final String DEFAULT_METHOD_PARAM = "_method";

同时提供一个接口可以让用户自定义参数名称:

	/*** Set the parameter name to look for HTTP methods.* @see #DEFAULT_METHOD_PARAM*/public void setMethodParam(String methodParam) {Assert.hasText(methodParam, "'methodParam' must not be empty");this.methodParam = methodParam;}

通过doFileter...执行拦截器:

①:表单请求携带参数:_method。

②:进入拦截器后检查请求是否是POST请求,请求是否错误

③:获取请求参数_method。

④:判断请求是否在给定的请求集合内:private static final List<String> ALLOWED_METHODS =       Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT.name(),             HttpMethod.DELETE.name(), HttpMethod.PATCH.name()));

⑤:将请求包装为:HttpMethodRequestWrapper  ,重写getmethod接口

@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)throws ServletException, IOException {//获取请求HttpServletRequest requestToUse = request;//判断是否为POST请求,请求是否错误if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {//获取请求方式参数_methodString paramValue = request.getParameter(this.methodParam);if (StringUtils.hasLength(paramValue)) {//将请求转为大写String method = paramValue.toUpperCase(Locale.ENGLISH);//判断请求是否在给定的请求集合中if (ALLOWED_METHODS.contains(method)) {//将请求包装一下,重写了请求的getmethod方法requestToUse = new HttpMethodRequestWrapper(request, method);}}}//放行过滤器filterChain.doFilter(requestToUse, response);}

重写getmethod:

	/*** Simple {@link HttpServletRequest} wrapper that returns the supplied method for* {@link HttpServletRequest#getMethod()}.*/private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {private final String method;public HttpMethodRequestWrapper(HttpServletRequest request, String method) {super(request);this.method = method;}@Overridepublic String getMethod() {return this.method;}}

扩展:如何自定义请求方法参数名称。

自定义配置类:

@Configuration
public class MyConfiger {@Beanpublic HiddenHttpMethodFilter hiddenHttpMethodFilter(){HiddenHttpMethodFilter hiddenHttpMethodFilter =new HiddenHttpMethodFilter();//自定义请求方式参数hiddenHttpMethodFilter.setMethodParam("_m");return hiddenHttpMethodFilter;}
}

http://www.lryc.cn/news/23914.html

相关文章:

  • 6 集成学习及Python实现
  • 如何编程实现从多数据库操作数据
  • LeetCode 147. 对链表进行插入排序 | C/C++版
  • 【QT进阶】第五章 QT绘图之自定义控件--仪表盘绘制
  • Java代码弱点与修复之——URL manipulation(URL操纵)
  • Sharding Sphere学习
  • 粗心小编被云拯救,那云上数据谁来拯救?
  • [git可视化软件]gitkraken平替:GitAhead
  • CentOS8基础篇8:使用systemctl管理NFS服务
  • Go defer用法
  • 产地证是什么,主要作用有哪些?
  • 王道计算机网络课代表 - 考研计算机 第一章 计算机网络体系结构 究极精华总结笔记
  • 数据处理 |遍历所有文件夹及子目录文件夹方法总结与实例代码详解
  • ProtoEditor - 如何在Unity中实现一个Protobuf通信协议类编辑器
  • 2022 OpenCV Spatial AI大赛前三名项目分享,开源、上手即用,优化了OAK智能双目相机的深度效果。
  • Android 蓝牙开发——HCI log 分析(二十)
  • flask入门-4.项目实战
  • java 1(概要、变量与运算符)
  • ​力扣解法汇总2363. 合并相似的物品
  • 2022年终总结-找回初心
  • Allegro如何打开或者关闭DFA规则设置操作指导
  • kind kubernetes 集群内如何通过 helm 部署定制化 Prometheus-Operator?
  • 流媒体付服务器 ZLMediaKit 学习记录
  • 2023年了还不会写软件测试简历吗,那就来看这里吧,怎么样才能更容易让HR看到你的简历
  • 第四阶段08-基于element-ui的vue2.0脚手架(续)
  • 数据库设计规范
  • 深入浅出PaddlePaddle函数——paddle.Tensor
  • docker删除已停止的容器
  • JS#1 引入方式和基础语法
  • 面了一个测试工程师,明显感觉他背了很多面试题...