4

In spring 3.2, we make use of this in our controllers:

@RequestHeader Map<String, String> headers

The issue is that the map of header keys can be any case, so we have to re-build the header map, forcing the keys to lowercase.

It would be nice if spring would consistently force lowercase or uppercase on the headers in the map in order to make it easier for header checking.

Looking into how to alter that behavior is difficult, same goes for the spring mvc test setups.

We could override the RequestHeaderMapMethodArgumentResolver but how?

Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
hansSolo
  • 61
  • 2
  • 7

1 Answers1

3

Yes, you can override RequestHeaderMapMethodArgumentResolver. Here's one way of doing it:

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;
    }
}
Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
  • works great, thanks! though I would back off that fugly class name ;] – hansSolo Jan 15 '13 at 22:48
  • I think you should have bigger worries than the performance impact of putting a handful of objects in an HashMap for each request. Anyways, if that really bothers you, you could create a class by copying the code in RequestHeaderMapMethodArgumentResolver and modify it to your needs. – Aleksander Blomskøld Jan 16 '13 at 06:28