I would like to use URLs in a path variable using Spring. However, I cannot get the desired behavior without having to encode the URL twice in the client.
In order to describe the problem I will take as reference the spring rest service tutorial, I changed the greeting method to use a path variable as follows:
@GetMapping("/greeting/{name}")
public Greeting greeting(@PathVariable(value = "name") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
If try to use the URL http://localhost:8080/greeting/test/slash
I would get a HTTP 404 NOT_FOUND response. But if I try something like http://localhost:8080/greeting/test%2Fslash
, I will get a 400 BAD_REQUEST response.
There is one answer in this question that suggests the use of the ALLOW_ENCODED_SLASH property, but it does not work. Like changing the property in the RestServiceApplication
class.
System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
Any suggestions?