Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to request complete query string in Spring MVC?

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!

like image 618
Rory Avatar asked Feb 14 '12 17:02

Rory


3 Answers

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();
}
like image 108
JB Nizet Avatar answered Sep 30 '22 02:09

JB Nizet


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

like image 34
Rafal Borek Avatar answered Sep 30 '22 03:09

Rafal Borek


Something like this you need to do:

    public void sendMessage(HttpServletResponse response,
                               @RequestParam("Session Id") String sessionId, HttpServletRequest request,..
    {
   String qString= request.getQueryString();
like image 31
kosa Avatar answered Sep 30 '22 01:09

kosa