5

I came across an interesting question while working with Spring and REST API and that problem is: Is the path limited to a certain number of characters in Spring?

The code is as follows

@RequestMapping(value = {REST_PREFIX + "/{key}"}, method = {RequestMethod.GET})
public DashboardItem getExceptionByKey(@PathVariable("key") String key, HttpServletRequest request, HttpServletResponse httpResponse_p) {
    log.info("URL is {}", request.getRequestURL());
    log.info("Key is {}", key);
    return InspectionUtils.getExceptionByKey(key);
}

An example of a key is

67E4D2C089CBCCA2A9732F6986124C6B10.243.2.107#0EEE5EAB06ED4FFF82A8934F7058860C#79A2F0C170A028A3B0410F0F16303F41

When sending the request I made sure to encode the URL and in my program the URL I am receiving is the following

/rest/exceptions/67E4D2C089CBCCA2A9732F6986124C6B10.243.2.107#0EEE5EAB06ED4FFF82A8934F7058860C#79A2F0C170A028A3B0410F0F16303F41

Thus I am receiving the hole key, but when it parses it, the variable key is only

67E4D2C089CBCCA2A9732F6986124C6B10.243.2

I thought that it may be special characters, but it doesn't look like it. My second guess is that there is a limitation for the length of the path.

So my question to you is if there is a limitation regarding the path or is there another problem?

Thank you

Ancuta
  • 183
  • 2
  • 6
  • 15
  • To answer your last question: yes, there is a limitation to the URL length -- browsers limit this to about 2000 characters. No, you are not hitting this limitation yet in Spring. – john16384 Mar 12 '17 at 11:38
  • I know about the 2000 character limitation. I was referring to the path variable, if it has a limitation – Ancuta Mar 12 '17 at 11:51
  • It has no limitation. Please show us where you are determining the key is cut off, I see no print statement, I only see another piece of code that may or may not be handling key incorrectly (`InspectionUtils.getExceptionByKey(key)`) – john16384 Mar 12 '17 at 11:52
  • My bad. I was printing the key before that call. I have updated the code in the original post. – Ancuta Mar 12 '17 at 11:57

3 Answers3

5

This is some kind of spring convention that treats everything after the last dot as a file extension and cuts it off. You could simply try adding a trailing / in your request mapping and the request. I.e. REST_PREFIX + "/{key}/"

For a more complicated but better solution if you are not the one calling your API see this question

Community
  • 1
  • 1
rob
  • 212
  • 3
  • 8
0

It must be the hash # character in your key which is not getting URL encoded. And since anything that comes after # in the URL represents different fragments of the HTML page, it never gets sent to the server.

On using javascript's encodeURIComponent() on your key i got:

67E4D2C089CBCCA2A9732F6986124C6B10.243.2.107%230EEE5EAB06ED4FFF82A8934F7058860C%2379A2F0C170A028A3B0410F0F16303F41

Notice the # character is now encoded. Try this, but you might need to decode it on your server.

Jeet Prakash
  • 625
  • 1
  • 7
  • 13
  • 1
    I have encoded the character in my Javascript code with encodedURIComponent() as that was my first problem. After that in my Java code the path I receive is correct, but the key is truncated. Also it gets automatically decoded on the server. My problem is that it is truncated not at the # but before – Ancuta Mar 12 '17 at 11:25
  • Eliminate in between sources of problems. Try do the call from PostMan, look at the Webserver access log to see what URL was actually sent. Make your endpoint `REST_PREFIX + "/**"` and look at what URL you're getting. – john16384 Mar 12 '17 at 11:45
  • I have done that as I mentioned before and the URL that I am getting on the Web server when logging it is good, the key is complete. When it is parsed, the key stops before the third dot. – Ancuta Mar 12 '17 at 11:50
  • Then log the key you are getting, or change the endpoint to `/**` as I suggested. Inject the HttpServletRequest and print the contents of getURI. – john16384 Mar 12 '17 at 11:51
  • I have also logged the key, and as I said the key is truncated before the third dot. I do not want to change the endpoint to /** and this will bring issues with my other code. A workaround I have found would be to use query parameters, but I am curious as to this problem. – Ancuta Mar 12 '17 at 11:53
  • @Ancuta follow the link in Rob's answer. Doing `/{key:.+}` on request mapping can help. – Jeet Prakash Mar 12 '17 at 11:55
  • Thank you. I was reading that now and it seems that is the solution I am looking for. – Ancuta Mar 12 '17 at 11:58
  • That's really archaic behaviour of Spring... luckily it can turned off. – john16384 Mar 12 '17 at 12:10
0

The # character has a special meaning in a URL. It is a fragment identifier, and it is used to jump to a certain part of a page (instead of landing at the top). To avoid this, encode the character as %23.

john16384
  • 7,800
  • 2
  • 30
  • 44
  • This is not the solution, as you can clearly see the path being cut off at the dot. – rob Mar 12 '17 at 11:43
  • And as I said in my post, the character is being encoded when sent to the server. – Ancuta Mar 12 '17 at 11:47
  • See my comment about eliminating other sources of problems. Spring is not the one cutting off your URL, as I know from experience it can handle URL's far far longer than this one. – john16384 Mar 12 '17 at 11:50