12

In Spring MVC, I can do this to get a value of items on the query string:

    public void sendMessage(HttpServletResponse response,
                           @RequestParam("Session Id") String sessionId,

But how to I get the complete querystring as one long string? I.e. I don't want individual parameters from it, I want the whole thing?

Many thanks!

Rory
  • 1,805
  • 7
  • 31
  • 45

4 Answers4

14

Add the HttpServletRequest as argument to the method, and get the query string from the request:

public void sendMessage(HttpServletRequest request,
                        HttpServletResponse response {
    String queryString = request.getQueryString();
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
4

If you don't want to use HttpServletRequest in your controller, you can create HandlerMethodArgumentResolver that resolves query string.

Example:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface QueryString {
}


public class QueryStringResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        Annotation[] parameterAnnotations = parameter.getParameterAnnotations();
        for (Annotation parameterAnnotation : parameterAnnotations) {
            if (QueryString.class.isInstance(parameterAnnotation)) {
                return true;
            }
        }

        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,                                 WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
        return request.getQueryString();
    }
}


<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="mypackage.QueryStringResolver"/>
    </mvc:argument-resolvers>
</mvc:annotation-driven>


public class MyController {
    @RequestMapping(...)
    public String someMethod(@QueryString String queryString) {
        ...
    }
}

3

Something like this you need to do:

    public void sendMessage(HttpServletResponse response,
                               @RequestParam("Session Id") String sessionId, HttpServletRequest request,..
    {
   String qString= request.getQueryString();
kosa
  • 65,990
  • 13
  • 130
  • 167
  • Curse you for being faster than me, but +1 for getting it right :) – Paul Feb 14 '12 at 17:10
  • ha ha! first one after refresh and recently did same as part of other project. so, quick answer – kosa Feb 14 '12 at 18:10
  • Bummer, your answer wasn't accepted. I've noticed the people with high rep will quickly throw out an answer to get the early timestamp and then go back and fill in the details. Maybe that's the trick to getting the big numbers...though I don't care enough to do such things. At least you and I know that you had the code down first :) – Paul Feb 14 '12 at 18:37
  • May be your curse became real (wink), ha ha!? I have observed same pattern too. Some how I like JB Nizet answers and his deep knowledge on lot of topics. I am learning a lot here, so no need to worry about reps. – kosa Feb 14 '12 at 19:44
  • I agree...I learn a lot just by answering questions. I've been saved many times (and learned even more) by people answering questions too. – Paul Feb 14 '12 at 19:51
0

The Controller itself knows the contents of the entire query string.

public ModelAndView handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception

You can then get the full query string from:

request.getQueryString();
AfroRick
  • 610
  • 5
  • 17