How to request complete query string in Spring MVC?

13,873

Solution 1

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();
}

Solution 2

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) {
        ...
    }
}

Solution 3

Something like this you need to do:

    public void sendMessage(HttpServletResponse response,
                               @RequestParam("Session Id") String sessionId, HttpServletRequest request,..
    {
   String qString= request.getQueryString();
Share:
13,873
chetan godiya
Author by

chetan godiya

please delete me

Updated on June 03, 2022

Comments

  • chetan godiya
    chetan godiya almost 2 years

    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!

  • Paul
    Paul over 12 years
    Curse you for being faster than me, but +1 for getting it right :)
  • kosa
    kosa over 12 years
    ha ha! first one after refresh and recently did same as part of other project. so, quick answer
  • Paul
    Paul over 12 years
    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 :)
  • kosa
    kosa over 12 years
    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.
  • Paul
    Paul over 12 years
    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.
  • msangel
    msangel over 10 years
    This is so... in Spring style.