2

We know @RequestParam is a good way to get query parameters like "?name=Tom" for example:

www.example.com/?name=Tom

and you can use

@RequestParam(value="name") String name

to get the key "name".

But how to get the query string without equal sign such as:

www.example.com/?1+1

There is no key-value pair in this case and I can't find the answer from the internet because basically query string is used by key-value case.

Btw, the Framework has to be SpringBoot with Java, and also html for Thymeleaf.

Jun
  • 31
  • 3
  • 1
    Here is one of the alternative way how you can do this. https://stackoverflow.com/questions/9281067/how-to-request-complete-query-string-in-spring-mvc – oisleyen Sep 11 '20 at 18:59

2 Answers2

2

You can get it from HttpServletRequest.getQueryString(). In the controller method, the HttpServletRequest is injected by SpringMVC.

@RestController
class WhatEverController {

   @GetMapping("whatever")
   public void whatEver(HttpServletRequest request,
                        HttpServletResponse response {
       String queryString = request.getQueryString();
   }
}

Chayne P. S.
  • 1,558
  • 12
  • 17
  • Thanks, it looks the same with @oisleyen's answer. I will use HttpServletRequest – Jun Sep 13 '20 at 11:02
0

Querystring from another source :

public static Map<String, String> getQueryMap(String query)  
{  
    String[] params = query.split("&");  
    Map<String, String> map = new HashMap<String, String>();  
    for (String param : params)  
    {  String [] p=param.split("=");
        String name = p[0];  
      if(p.length>1)  {String value = p[1];  
        map.put(name, value);
      }  
    }  
    return map;  
} 

You can use

Map params=getQueryMap(querystring);
 String id=(String)params.get("id");
 String anotherparam=(String)params.get("anotherparam");