Here is my controller:
@RequestMapping(method = RequestMethod.GET, value="/test/**", headers="Accept=*/*")
public @ResponseBody ResponseEntity<byte[]> getRequest(HttpServletRequest request)
{
System.out.println((String) request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE ));
}
Whenever I want to create GETrequest looking like this: localhost:8080/test/some/request/given/in My system out write in console:
some/request/given/in
As I want it to. Problem comes when I have instead of slash - / symbol %2F or %2f. When I have those symbols in my path request is not handled by controller at all.
Is there any way to fix this?
Typically most of the application server will not not treat %2F as / if %2F is found in URL. But as the specs declare %2F is one of the ways a path delimiter can be specified, for handling this scenario, tomcat provides system level property
For tomcat, if
org.apache.tomcat.util.buf. UDecoder.ALLOW_ENCODED_SLASH = true
then %2F and %5C will be permitted as path delimiters. Refer to the documentation. This can be set as JAVA_OPTS in tomcat batch files as follows
set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG% -Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
This is specifically for Tomcat. I am sure there would be similar arrangement for other application servers as well.
You need to use URL encoder and decoder to send the path params with special chars such as / and -. In your server side code, you just need to put:
String result = URLDecoder.decode(url, "UTF-8");
and then fetch the params.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With