Rubing Yang
Rubing Yang

Reputation: 95

Is there any situation QueryString is present but HttpServletRequest.getParameterMap() is empty?

I have encouterd an odd situation while we are doing press testing in our test env. When the app load is high, the Query String will missing occasionally and the Spring will throw MissingServletRequestParameterException. In order to find the root cause , I add some logs at the foremost Filter(code is shown below), and something weired happened.

    public static void printRequestParameter(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        log.info("Request URI : {}, method = {} , query string = {}", request.getRequestURI(), request.getMethod(), request.getQueryString());
        if (MapUtils.isNotEmpty(parameterMap)) {
            parameterMap.forEach((k, v) -> {
                log.info("Request parameter name = {}, value = {}", k, ArrayUtils.isEmpty(v) ? Strings.EMPTY : Arrays.stream(v).collect(Collectors.joining(COMMA)));
            });
        }
    }

The request.getParameterMap() is empty, but , the query string is not empty , and I got a log like :

Request URI : /a/b/c, method = POST , query string = foo=bar.

But no logs like:

Request parameter name = foo , value = bar

And our Controller use @RequestParam() String foo to receivce the parameter , finally , the Spring throws

MissingServletRequestParameterException Handler org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'foo' is not present
    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:204)
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:114)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
    at org.springframework.web.method.support.InvocableH

I just wonder, why the parameter in query string is not contained in parameterMap? Note: The odd behavior is only happened occasionally, at most time it's just works. My spring boot version is 2.3.9.RELEASE and the embedded tomcat version is 9.0.43.

Any help is appreciated!

Upvotes: 1

Views: 941

Answers (2)

Rubing Yang
Rubing Yang

Reputation: 95

Finally, It's @Async issue , some guy put this annotation on the Controller class

Upvotes: 1

Piotr P. Karwasz
Piotr P. Karwasz

Reputation: 16055

Since the specification does not allow ServletRequest.getParameterMap to throw any exception, any failure in parameter parsing will cause the parameter list to be empty.

To detect this situation you can log the "org.apache.catalina.parameter_parse_failed_reason" attribute of your request.

Examples of query strings that should fail:

  • ?=noname,
  • ?urlEncoding=%ue

Upvotes: 2

Related Questions