首页 > 其他分享 >springboot开发日记(11)——请求参数处理

springboot开发日记(11)——请求参数处理

时间:2023-02-12 11:58:24浏览次数:47  
标签:11 String request Rest 日记 public POST method springboot

请求映射

Rest风格(使用HTTP请求方式动词来表示对资源的操作

 /user   GET-获取用户 DELETE-删除用户 PUT-修改用户 POST-保存用户 在index.html中添加表单标签:
<form action="/user" method="post">
    <input value="REST-post test" type="submit">
</form>
<form action="/user" method="get">
    <input value="REST-get test" type="submit">
</form>
<form action="/user" method="delete">
    <input value="REST-delete test" type="submit">
</form>
<form action="/user" method="put">
    <input value="REST-put test" type="submit">
</form>

我们会发现method属性中只有post和get两个值,但是我们先按照Rest风格的格式进行赋值。

在HelloController.java中编写对应处理方法

@RequestMapping(value = "/user",method = RequestMethod.GET)
public String getUser(){
    return "GET-张三";
}

@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-张三";
}

进入网站测试,依次点击四个测试按钮发现前两个使用post和get方法的结果正确,后两个方法的显示结果错误。原因是后两个方法目前不支持,所以直接按照默认值get执行,为了解决这个问题,我们不妨再次看一下源码:仍在是在WebMvcAutoConfiguration.class这个类下方寻找,找到一个名为public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter()的方法,我们可以通过这个过滤器启用Rest风格,同时也说明springboot是默认配置这个过滤器的。再查看OrderedHiddenHttpMethodFilter的源码,发现是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) {
        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);
            }
        }
    }

    filterChain.doFilter((ServletRequest)requestToUse, response);
}
 我们发现最外层判断条件是当method的值为POST时才生效,String paramValue = request.getParameter(this.methodParam); 这句意思是获取名为methodParam的值。观察上文发现methodParam被规定了值:private String methodParam = "_method";也就是说我们需要在form标签中添加一个名为_method的隐藏域,这个隐藏域的值则是我们希望该form标签生效的Rest风格。修改delete和put两个风格的form表单:
<form action="/user" method="post">
    <input type="hidden" name="_method" value="DELETE">
    <input value="REST-delete test" type="submit">
</form>
<form action="/user" method="post">
    <input type="hidden" name="_method" value="PUT">
    <input value="REST-put test" type="submit">

 此时我们再次执行测试发现仍然没有生效,于是我们再去查看源码:发现在WebMvcAutoConfiguration.class这个类下的public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter()方法的注释有所要求:

@Bean
@ConditionalOnMissingBean({HiddenHttpMethodFilter.class})
@ConditionalOnProperty(
    prefix = "spring.mvc.hiddenmethod.filter",
    name = {"enabled"}
)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
    return new OrderedHiddenHttpMethodFilter();
}
这两个@Conditional注解表示如果开发者没有配置HiddenHttpMethodFilter.class这个类则springboot会自动配置,重点在后面这个@Conditional注解,我们只有配置了规定的属性这个过滤器才会生效。如果没有配置该属性则默认不生效,那么我们需要在yaml文件中进行配置。
spring:
  mvc:
    hiddenmethod:
      filter:
        enabled: true

 开启过滤器后我们再进行测试发现功能正常,配置成功。

Rest原理(表单提交要使用REST的时候)

  • 表单提交会带上_method=PUT
  • 请求过来被HiddenHttpMethodFilter拦截
    • 请求是否正常,并且是POST
      • 获取到_method的值。
      • 兼容以下请求;PUT.DELETE.PATCH
      • 原生request(post),包装模式requesWrapper重写了getMethod方法,返回的是传入的值。
      • 过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的。

我们可以直接在Controller类使用四种Rest风格的注解:

@GetMapping
@PostMapping
@DeleteMapping
@PutMapping

注意:使用这四种注解也需要开启过滤器 

标签:11,String,request,Rest,日记,public,POST,method,springboot
From: https://www.cnblogs.com/tarorat/p/17113369.html

相关文章

  • Springboot打印接口耗时
    aop切面的方式实现思路引入aop依赖自定义注解定义切面,采用环绕通知代码实现引入依赖<!--aop--><dependency><groupId>org.springframework.boot</groupId>......
  • SpringBoot实现多线程
    多线程方式一:实现AsyncConfigurer接口配置类实现接口AsyncConfigurer,返回一个ThreadPoolTaskExecutor线程池对象。@Configuration@EnableAsyncpublicclassAsyncCon......
  • 『Solution』未来日记
    未来日记纪念第一道自己独立想出Solution的Ynoi题意:支持区间定值修改和区间第\(k\)大,数据范围\(10^5\),空间限制\(512MB\),时间限制\(1000ms\)显然1e5是分块,考虑怎么做区......
  • 11th Feb
    1、单例模式:---饿汉式 单例有问题,结果不一样:32min的时候就不明白了静态内部类都不会被加载???bilibili.com/video/BV1UA411G7YN/?p=2&spm_id_from=pageDriver&vd_so......
  • 从空项目开始建立MFC程序导致1189错误解决办法
    错误提示:严重性代码说明项目文件行源禁止显示状态工具错误(活动) E0035 #error指令:BuildingMFCapplicationwith/MD[d](CRTdllversion)requiresMFC......
  • springboot 配置redis集群 JedisCluster 3主3从 哈希槽模式
    packagecom.estate.util;importredis.clients.jedis.*;importjava.util.HashSet;importjava.util.Set;publicclassRedisClient{privatestaticJedis......
  • win10 0x0000011b共享打印机无法连接
    提供打印共享的win10由于升级后,其它需要使用打印机的windows电脑在连接共享的打印机时会有这个报错网上说在这台打印服务器上删除补丁即可,可以我没有找到,如:KB5005565补丁......
  • day11- 20.有效括号|1047.删除字符串中所有相邻重复项
    20.有效括号leetcode题目链接:https://leetcode.cn/problems/valid-parentheses/题目描述:给定一个只包括'(',')','{','}','[',']'的字符串s,判断字符串是否有效。有效字......
  • 关于win11没有gpedit.msc(本地组策略管理)的解决方案
    转载自https://blog.csdn.net/Xingchen0101/article/details/128943201在本地部署一个bat文件里面粘贴以下文本pushd"%~dp0"dir/bC:\Windows\servicin......
  • 2023/2/11
    今天终于把驾照拿到手了,很兴奋,也是第一天开始实现按昨日计划行动的一天,但是第一天就碰到了意外情况,对象昨晚开始就胃不舒服,今早发现是胃溃疡的症状所以带去看了医生,不过好......