I have an api:
http://localhost:8080/mylocalserver/#/reports/{parameter1}/{parameter2}
now the first parameter is parameter1 = "12345 / EF" and the second parameter is parameter2 = "Text"
I am using Spring Rest Controller to create api. When a query is executed with the following parameters above, it shows
Request Not Found
.
I tried to encode and decode the parameters using URLEncoder.encode
, as well as using UriUtils.encode(param, StandardCharsets.UTF_8.name())
, but still getting the same error.
Below is the code I tried.
Suppose I have two variables from user input
parameter1
andparameter2
Now I am creating a URL with the following parameter
private void createApi(String parameter1, String parameter2) {
try {
String uri = " http://localhost:8080/mylocalserver/#/reports/"+encode(parameter1)+"/"+encode(parameter2)+" ";
ServletRequestAttributes requestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
HttpServletResponse response = requestAttributes.getResponse();
request.getRequestDispatcher(uri).forward(request, response);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
And my coding method:
public String encode(String param) {
try {
UriUtils.encode(param, StandardCharsets.UTF_8.name())
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return param;
}
Note. I am using Tomcat 8.5
source