0

I have spring application

which has URL of format

{URL}/locations/USA#CA#92121

I want to get 'USA#CA#92121' as one parameter in my REST controller .. How can I achieve that?

Currently its only giving "USA" when I use @PathVariable

user1228785
  • 512
  • 2
  • 6
  • 19

2 Answers2

0

Your URL will need to be url encoded. You can use URLEncoder in Java 8 for this.

See Java URL encoding of query string parameters

basically

String q = "random word £500 bank $";
String url = "http://example.com/query?q=" + URLEncoder.encode(q, "UTF-8");
Community
  • 1
  • 1
Thom
  • 14,013
  • 25
  • 105
  • 185
0

You need to encode the URL so that it can handle all types of characters including spaces and special characters like #, $ etc..

The best way to do that would be to use an URLEncoder and then to get back the value that you want, you will need to use the URLDecoder

For Example:

package com.test;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLDecoder;

public class EncodeURL {

    public static void main(String args[]){
        string url = "{URL}/locations/USA#CA#92121";
        try {
            string encodedString = URLEncoder.encode(url, "UTF-8");
            System.out.println("Encoded String: " + encodedString);
            System.out.println("Decoded String: " + URLDecoder.dencode(encodedString, "UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
    }
}
Sumit
  • 2,189
  • 7
  • 32
  • 50