package com.xbhong.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@RestController
public class myContro {
@RequestMapping(value = “/user”,method = RequestMethod.GET)
public String getUser(){
return “GET-张三”;
}
// @PostMapping("/user")
@RequestMapping(value = “/user”,method = RequestMethod.POST)
public String saveUser(){
return “POST-张三”;
}
@RequestMapping(value = “/user”,method = RequestMethod.PUT)
public String putUser(){
return “PUT-张三”;
}
@RequestMapping(value = “/user”,method = RequestMethod.DELETE)
public String deleteUser(){
return “DELETE-张三”;
}
}
测试功能是否可用:
=========
观察效果:
可以看到最后两个请求都变成Get的请求了。由此我们可以引出来如下问题:
-
为什么不同的请求会出现相同的结果
-
怎么实现的才能完成我们的功能
后面的文章就是围绕上述的问题进行展开的。
解决问题:
=====
像之前的SpringMVC中的表单请求通过HiddenHttpMethodFilter实现的,这样我们查一下在SpringBoot中是怎么样的。
默认双击Shift键,输入WebMvcAutoConfiguration这个类
找到默认配置的过滤器:
@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
@ConditionalOnProperty(prefix = “spring.mvc.hiddenmethod.filter”, name = “enabled”, matchIfMissing = false)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new OrderedHiddenHttpMethodFilter();
}
通过
OrderedHiddenHttpMethodFilter进入父类HiddenHttpMethodFilter:
在使用之前,我们需要将后两个的请求方式改写成post方式,并且需要在请求的时候传入一个 _method 方法(设置隐藏域);
<input value="REST-PUT提交"type=“submit” />
为什么要这样设置呢?我们到HiddenHttpMethodFilter中看下。
流程:
-
第一步保存了传入的请求
-
当该请求是post,并且请求没有异常,才能进入下面方法,不是Post请求将直接通过过滤器链放行。
-
获取请求中的参数(this.methodParam)
-
DEFAULT_METHOD_PARAM = _method获得(作为真正的请求方式)
然后再测试,发现还是没有实现。
我再回到WebMvcConfiguration中:
@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
@ConditionalOnProperty(prefix = “spring.mvc.hiddenmethod.filter”, name = “enabled”, matchIfMissing = false)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new OrderedHiddenHttpMethodFilter();
}
首先该类存在于容器中。
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
当容器中不存在 HiddenHttpMethodFilter 这个类的时候,下面内容开启(条件装配);
@ConditionalOnProperty(prefix = “spring.mvc.hiddenmethod.filter”, name = “enabled”, matchIfMissing = false)
表示:绑定的配置文件中:
spring.mvc.hiddenmethod.filter 名字为enable是默认不开启的(后续版本可能开启)。这样我们就找到了问题的所在。
所以我们需要在配置文件中配置(两种方法都可以)。
yaml:
spring:
mvc:
hiddenmethod:
filter:
enabled: t
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
rue
properties:
spring.mvc.hiddenmethod.filter.enabled=true
重启项目:成功解决。
源码分析:
=====
主要是分析doFilterInternal:
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
HttpServletRequest requestToUse = request;
if (“POST”.equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
String paramValue = request.getParameter(this.methodParam);
if (StringUtils.hasLength(paramValue)) {
String method = paramValue.toUpperCase(Locale.ENGLISH);
if (ALLOWED_METHODS.contains(method)) {
requestToUse = new HttpMethodRequestWrapper(request, method);
}
}
}