3

My controller has a method like below:

@ResponseBody
public String test(@RequestParam("r") String r){
String requestURI = request.getQueryString();
// What I want: abc+def%2Cfjg
System.out.println(requestURI.split("=")[1]);
// This is not what I want: abc def,fjg
System.out.println(r);
}

How can I let Spring boot know that the RequestParameter must not be decoded? i.e not converting + to space, %2C to comma etc..

Tim Falony
  • 125
  • 3
  • 13
  • Could you elaborate on your reasons for wanting this? – Kayaman Jul 19 '17 at 19:18
  • + is an acceptable character according to URI RFC and I want to leverage that as a way to separate entities that belong to a request parameter. – Tim Falony Jul 19 '17 at 19:22
  • You mean you want a parameter to have multiple values? Why not use the standard way then? – Kayaman Jul 19 '17 at 19:28
  • Standard way? My parameter can have one or more values and I split by , so to avoid confusion if the parameter has %2C then it will be considered as a character inside one of the values and not split by ,. – Tim Falony Jul 19 '17 at 19:35
  • Yes, the standard way. You could take `HttpServletRequest` as a parameter and use its `getParameterValues()` method (or change your parameter to `String[]`). Doing basic parsing like that yourself is a waste of your time. – Kayaman Jul 19 '17 at 19:39
  • if I change to String[] how to send the request? Can I separate them by , and it will automatically parse it? What if one of the values has a comma in it? – Tim Falony Jul 19 '17 at 20:43
  • How are you sending the request now? If you put the parameter name twice, it will parse them as separate values. If you're using Javascript, you can just send an array. – Kayaman Jul 19 '17 at 20:49
  • Example request: /localhost/app?r=value1,value2,val+rfi for this URL when I want to get the value of r I want it to be: value1,value2,val+rfi but I'm getting it as value1,value2,val rfi – Tim Falony Jul 19 '17 at 21:16
  • https://stackoverflow.com/questions/24059773/correct-way-to-pass-multiple-values-for-same-parameter-name-in-get-request or even better, don't send the data in the query string but use JSON for example. – Kayaman Jul 20 '17 at 09:07

1 Answers1

-1

With the use of decode method you can decode your url.

private String decode(String value) {
    String decoded 
      = URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
    return decoded;
}

If you want to encode it then use encode method.

private String encode(String value) {
    String encoded = 
      URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
    return encoded;
}
Vinit Solanki
  • 1,863
  • 2
  • 15
  • 29