java – @RequestHeader地图映射默认情况?
作者:互联网
在3.2版本中,我们在控制器中使用它:
@RequestHeader Map< String,String>头
问题是标题键的映射可以是任何情况,因此我们必须重新构建标题映射,将键强制为小写.
如果spring一直强制在地图中的标题上使用小写或大写,以便更容易进行标题检查,那就太好了.
研究如何改变这种行为很困难,春季mvc测试设置也是如此.
我们可以覆盖RequestHeaderMapMethodArgumentResolver但是如何?
解决方法:
是的,您可以覆盖RequestHeaderMapMethodArgumentResolver.这是一种方法:
import org.apache.commons.collections.map.CaseInsensitiveMap;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.annotation.RequestHeaderMapMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import java.util.Map;
public class FixRequestHeaderMapMethodArgumentResolverConfigurer implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String s) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String s) throws BeansException {
if(bean instanceof RequestHeaderMapMethodArgumentResolver) {
return new RequestHeaderMapMethodArgumentResolver() {
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
return new CaseInsensitiveMap((Map)super.resolveArgument(parameter, mavContainer, webRequest, binderFactory));
}
};
} else
return bean;
}
}
标签:java,spring-mvc,spring,spring-test-mvc 来源: https://codeday.me/bug/20190629/1329422.html