简体   繁体   中英

Spring - ServletWebRequest.getHeaderValues() not returning all headers

I have my controller function setup like so

@RequestMapping(value = "/test/index.html", method = RequestMethod.GET)
public ModelAndView prepareView(HttpServletRequest request, @RequestHeader HttpHeaders header) 

I was getting a null pointer exception from this at

 org.springframework.web.method.annotation.RequestHeaderMapMethodArgumentResolver.resolveArgument(RequestHeaderMapMethodArgumentResolver.java:72) 

so I decompiled this class file and stepped through in debug mode. The problematic section of the code looks like this:

/* 70 */       for (Iterator<String> iterator = webRequest.getHeaderNames(); iterator.hasNext();) {
/* 71 */         String headerName = (String)iterator.next();
/* 72 */         for (String headerValue : webRequest.getHeaderValues(headerName)) {
/* 73 */           result.add(headerName, headerValue);
/*    */         }
/*    */       }

webRequest is an object of type NativeWebRequest . So in line 70 I can see that webRequest.getHeaderNames() is returning the request headers i see in chrome network tab for example.

Apparently the load balancer (netscaler) is inserting some headers after the request is created (i'll call this x-model ) that I cannot see in chrome.

Following this trace I see all the standard RequestHeaders go through and be added with no issues (ie Accept , Cookie , Host , etc). However, eventually the iterator on line 70 reaches the x-model header. Then in line 72, webRequest.getHeaderValues('x-model') is called, and this returns null as there is no header called x-model is found. This causes a null pointer on the enhanced foreach.

Why is there a discrepancy between what is returned by ServletWebRequest.getHeaderNames() and ServletWebRequest.getHeaderValues(headerName) ? What can I do to resolve this issue?

I have noticed that when I look at the iterator created in line 70, there is a keySet hashmap containing [cookie, connection, accept-language, host, accept, user-agent, accept-encoding, referer, x-model, x-tenant, upgrade-insecure-requests] . However, the table entry does not contain an entry for x-model. Please see attached image for the 2 objects i refer to

I am using Java 7 and Spring 4.0.7

在此处输入图片说明

What is the difference between table and entryset here? Why does entryset contain the x-model entry but table does not?

所以问题实际上是因为有人向HttpServletRequestWrapper.getHeaderNames()添加了覆盖,这是Spring用于检索标头名称列表以支持包装标头的方法,但并未覆盖Spring所使用的HttpServletRequestWrapper.getHeaders()找到标题的值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM