How to request complete query string in Spring MVC?

前端 未结 4 1786
感情败类
感情败类 2021-01-11 17:33

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

    public void sendMessage(HttpServletResponse response,
                                  


        
相关标签:
4条回答
  • 2021-01-11 17:56

    Something like this you need to do:

        public void sendMessage(HttpServletResponse response,
                                   @RequestParam("Session Id") String sessionId, HttpServletRequest request,..
        {
       String qString= request.getQueryString();
    
    0 讨论(0)
  • 2021-01-11 18:13

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

    0 讨论(0)
  • 2021-01-11 18:17

    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();
    }
    
    0 讨论(0)
  • 2021-01-11 18:17

    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();
    
    0 讨论(0)
提交回复
热议问题