好吧,我搞定了。答案有点棘手,所以如果有人遇到此类问题,我想在这里注册它。
@Neil McGuigan 在他的评论中为我指出了正确的方向,但一开始我并没有注意。这里的罪魁祸首是我们的远程应用程序端的非常、非常、非常糟糕的 API 设计。
_method是用于指定非标准HTTP动词的字段,例如PUT、PATCH、DELETE、TRACE等。此字段由HiddenHttpMethodFilter 过滤,HttpServletRequest 使用此“新”方法包装。您可以在 the file's source 了解它是如何工作的。
因为我希望这个 _method 字段通过过滤器而不修改整个请求(并导致错误,因为在 `RequestMethod 上没有 ping 或 message 这样的动词)我首先必须停用过滤器.这可以通过两种方式完成:
我可以阻止 Spring Boot 自动配置 Spring MVC,在加载 ApplicationContext 时跳过加载 WebMvcAutoConfiguration。正如您所想象的那样,这是一个 BIG、BIG、BIIIIG NO,因为things 可能会发生。
-
我可以使用FilterRegistrationBean 来禁用坏过滤器。非常简单直接,这是我选择使用的方法:
@Bean
public FilterRegistrationBean registration(HiddenHttpMethodFilter filter) {
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
registration.setEnabled(false);
return registration;
}
最后但同样重要的是,我决定给HiddenHttpMethodFilter 一个小扩展,以某种方式改进请求是如何通过的。 Java EE Spec 在 Servlet Spec Commandments 中非常清楚地指出:
你不应该改变你的请求。你必须尊重发件人(类似的东西)
虽然我同意这一点,但为了我的精神稳定,我还是决定改变它。为此,我们可以使用简单的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);
}
}
}
所以,当标头在我的whatoolsMethods 列表中时,它的作用是用新的x-whatools-method 标头包装请求。有了这个,我可以轻松地使用@RequestMapping 的headers 属性并将请求映射到正确的控制器方法。
回到最初的问题,我几乎可以肯定(好吧,99,95% 应该完全确定,但我们不要冒险)@RequestMapping 上的params 属性仅适用于 GET URI 上的请求参数,例如http://foo.bar/?baz=42。它无法过滤请求正文中发送的参数。
感谢尼尔的指导,即使很小!我希望这对某人有所帮助。