Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limitation of path in Spring

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

like image 271
Ancuta Avatar asked Mar 12 '17 11:03

Ancuta


People also ask

Is path variable mandatory?

Using @PathVariable required attribute From Spring 4.3. 3 version, @PathVariable annotation has required attribute, to specify it is mandatorily required in URI. The default value for this attribute is true if we make this attribute value to false, then Spring MVC will not throw an exception.

What is the use of path variable in Spring?

@PathVariable is a Spring annotation which indicates that a method parameter should be bound to a URI template variable. It has the following optional elements: name - name of the path variable to bind to. required - tells whether the path variable is required.

How does Spring boot handle 1000 requests per second?

To handle high traffic, you should setup Load Balancer with multiple node/instances. Better to go with Auto Scaling on Cloud server. It will increase the instances as per high load (number or request) and again decrease the instances when there will be low number of requests. Which is cost effective.

How do I send multiple path variables in Spring boot?

Try this: @RequestMapping(value = "/{lang}/{count}/{term}", method=RequestMethod. GET) public ResponseEntity<?> getSomething(@PathVariable("lang") String lang, @PathVariable("count") String count, @PathVariable("term") String term) { // Your code goes here. }


1 Answers

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

like image 177
rob Avatar answered Oct 13 '22 03:10

rob