栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用表单主体的Spring MVC Controller方法映射

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用表单主体的Spring MVC Controller方法映射

是的,我知道了。答案有些棘手,所以如果有人遇到这种问题,我想在这里注册。

@Neil McGuigan在他的评论中为我指出了正确的方向,但起初我没有注意。罪魁祸首是我们的远程应用程序方面非常 非常 糟糕的API设计。

_method
是用于指定非标准HTTP动词诸如场
PUT
PATCH
DELETE
TRACE
等。该字段由过滤,
HiddenHttpMethodFilter
HttpServletRequest
使用此“新”方法进行包装。您可以在文件源中看到其工作方式。

因为我希望该

_method
字段通过过滤器而不修改整个请求(并且由于没有诸如RequestMethod
ping
或之类的动词而导致错误
message
),所以我首先必须停用过滤器。这可以通过两种方式完成:

  1. 我可以阻止Spring Boot自动配置Spring MVC,

    WebMvcAutoConfiguration
    而在加载时跳过
    ApplicationContext
    加载。正如你能想象这是一个很大很大的,BIIIIG NO 因为,事情可能发生。

  2. 我可以使用

    FilterRegistrationBean
    来禁用错误的过滤器。非常简单明了,这是我选择使用的方法:

@Beanpublic FilterRegistrationBean registration(HiddenHttpMethodFilter filter) {FilterRegistrationBean registration = new FilterRegistrationBean(filter);registration.setEnabled(false);return registration;}

最后但并非最不重要

HiddenHttpMethodFilter
的一点是,我决定稍作扩展,以某种方式 改进 请求的处理方式。Java
EE规范在Servlet规范指令中非常清楚地指出:

不应该 改变您的要求。您必须尊重发件人(类似的东西)

尽管我同意这一点,但是为了我的心理稳定,我还是决定更改它。为此,我们可以使用simple

HttpServletRequestWrapper
方法,重写所选方法,并使用包装的部分过滤原始请求。我最终做了这样的事情:

public class WhatoolsHiddenHttpMethodFilter extends OrderedHiddenHttpMethodFilter {    @Override    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {        String paramValue = request.getParameter(OrderedHiddenHttpMethodFilter.DEFAULT_METHOD_PARAM);        if("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) { String method = paramValue.toUpperCase(Locale.ENGLISH); List<String> whatoolsMethods = Arrays.asList("ping", "message", "carbon", "media", "media_carbon", "ack"); if(whatoolsMethods.contains(paramValue)){     WhatoolsHiddenHttpMethodFilter.HttpMethodRequestWrapper wrapper = new WhatoolsHiddenHttpMethodFilter  .HttpMethodRequestWrapper(request, "POST", paramValue);     filterChain.doFilter(wrapper, response); } else {     WhatoolsHiddenHttpMethodFilter.HttpMethodRequestWrapper wrapper = new WhatoolsHiddenHttpMethodFilter  .HttpMethodRequestWrapper(request, method, null);     filterChain.doFilter(wrapper, response); }        } else { filterChain.doFilter(request, response);        }    }    private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {        private final String method;        private final String whatoolsMethod;        public HttpMethodRequestWrapper(HttpServletRequest request, String method, String whatoolsMethod) { super(request); this.method = method; this.whatoolsMethod = whatoolsMethod;        }        @Override        public String getMethod() { return this.method;        }        @Override        public String getHeader(String name) { if("x-whatools-method".equals(name)){     return this.whatoolsMethod; } return super.getHeader(name);        }        @Override        public Enumeration<String> getHeaderNames() { List<String> names = Collections.list(super.getHeaderNames()); if(this.whatoolsMethod != null){     names.add("x-whatools-method"); } return Collections.enumeration(names);        }    }}

因此,这是

x-whatools-method
在标头位于我的
whatoolsMethods
列表中时用新标头包装请求。这样,我可以轻松使用
@RequestMapping
headers
属性并将请求映射到正确的控制器方法。

回到最初的问题,我几乎可以肯定(99.95%应该完全确定,但不要冒险),该

params
属性
@RequestMapping
仅对GET
URI上的请求参数有效
http://foo.bar/?baz=42
。它不能过滤请求正文上发送的参数。

感谢尼尔的指导,即使很小!我希望这可以帮助别人。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647713.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号