Front slash(/) and special characters in rest url [duplicate] - spring-boot

This question already has answers here:
#PathVariable in SpringBoot with slashes in URL
(4 answers)
Closed last month.
Restful web services : GetMapping url has a variable which should be passed as a path variable but that variable contains front slash and special characters in it. Any suggestions on how to handle that in springboot.
#GetMapping(value = “/test/{id}”)
Public TestClass getData(#PathVariable(“id”)){
return testService.getDataService(id);
}
Here the Path variable “id” value is “00/01*1”

Look a this [https://www.baeldung.com/spring-slash-character-in-url][1],
Maybe use the #RequestParam("param") annotation for your request handler

Related

Laravel optional parameter with slash in route [duplicate]

This question already has answers here:
Optional parameter in the middle of a route
(3 answers)
Closed 3 years ago.
I'm having trouble creating ONE laravel route that has an optional parameter. The following achieves the behaviour I want:
Route::get('/{locale}/donate', 'MyController#index')->name('donation.index');
Route::get('/donate', 'MyController#index')->name('donation.index');
Both the urls /fr/donate and /donate will load the MyController index(). However, when I do this:
Route::get('/{locale?}/donate', 'MyController#index')->name('donation.index');
The /donate will not load the MyController index(). How do I make the locale argument an optional segment in the url?
optional parameters can be located only at the end of the url
Route::get('/donate/{locale?}', 'MyController#index')->name('donation.index');
In my opinion there is no way to define the parameter as optional. Here are other ways to solve your problem.
First
Create a subdomain for that
Second move your optional parameter to the last. Now your route will look like this
Route::get('/donate/{locale?}', 'MyController#index')->name('donation.index');

What is String:.+ in spring request mapping's path param

While I was modifying the code written by other developer I come across an end point #RequestMapping(value = "/ICD/{icdcode:.+} and wanted to know what is :.+ in the path variable.
This has already been answered
Spring MVC #PathVariable getting truncated
Spring MVC #PathVariable with dot (.) is getting truncated
Spring - Path variable truncate after dot - annotation
Basically, it is a regular expression. Spring considers that anything behind the last dot is an extension and get rid of it.
If you have a mapping to /somepath/{email} and try /somepath/test#gmail.com the value for the path parameter email will be test#gmail
Using the regular expression {pathparam:.+} everything is considered part of the value, even what is behind the last dot.

Accessing rest of URI as PathVariable [duplicate]

I have a use case. Spring MVC REST Url receive content using the GET method code is as follows:
#RequestMapping("/q/{key}")
public String query(#PathVariable() String key, Model model){
//todo`
}
But the front end of such a request: /q/SiGeC%2FSi%E5%BC%82%E8%B4%A8%E7%BB%93. %2F decoded character /. The controller can not match mapping request.
How should I do?
You can include regular expressions in your path variable as such:
#RequestMapping("/q/{key:.*}")
This will grab EVERYTHING after the /q/. Or you can make it a more specific regex to match the pattern you are actually expecting.
Annotations of # PathVariable may not be able to solve this problem.Last use the workaround is resolved.Code is as follows:
#RequestMapping("/q/**")

What is the meaning of {id:.+} in a Spring MVC requestmapping handler method?

I came across a method in the controller. What is this id:.+ ??
#RequestMapping(value="/index/{endpoint}/{type}/{id:.+}", method=RequestMethod.POST, consumes=kContentType, produces=kProducesType)
#ResponseBody
public String indexData(#PathVariable(value="endpoint") String endpoint, #PathVariable(value="type") String type, #PathVariable(value="id") String id, #RequestBody String body, HttpServletRequest request) {
logger.debug("In web controller for endpoint " + endpoint);
return indexController.indexData(endpoint, type, id, body, getSecurityContextProvider(request));
}
The syntax of a path variable in a spring MVC controller requestmapping is {variable_name:regular_expression}. You can optionally omit the regular expression, which leads to what you see more often, {id}.
So, for the example /index/{endpoint}/{type}/{id:.+} the variable name is id and the regular expression is .+ (see below reference to spring docs).
The regular expression .+ is stating "match the metacharacter . one or more times". The '.' metacharacter represents any character including white space (though some implementations will not match newlines). See http://en.wikipedia.org/wiki/Regular_expression
The regular expression is being used to help Spring determine the value of the variable because you can have complex variable names or there might be other important information at the end of the path that would otherwise get sucked into the variable value if Spring just said "go until the end of the path" (eg. filename extensions or path variables).
It's possible that in your example, the id variable can contain special characters that would otherwise cause Spring to terminate the variable prematurely. I've run into this problem before when trying to use a filename that contained a file extension (foobar.jpg). Spring would return only the "foobar" part of the variable because Spring was assuming I wanted it to terminate the variable value at the period delimiter. So, in this case, to make sure that "id" matches the full value, you put the regex that tells Spring to go ahead and match everything between the last forward slash and the end of the path. SO Reference: Spring MVC #PathVariable getting truncated
Here's the excerpt from the Spring docs that deals with complex variable matching:
Sometimes you need more precision in defining URI template variables. Consider the URL "/spring-web/spring-web-3.0.5.jar". How do you break it down into multiple parts?
The #RequestMapping annotation supports the use of regular expressions in URI template variables. The syntax is {varName:regex} where the first part defines the variable name and the second - the regular expression."
Here is their (fairly complex) example:
#RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
public void handle(#PathVariable String version, #PathVariable String extension) {
// ...
}
Source: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
The example they provide shows how you perform complex mappings from requests to controller method paramters that you wouldn't be able to without using a regular expression.

How to get JAX-RS (Jersey) to use a specific Locale when parsing url parameters and JSON? (Ideally inferred from the "Accept-Language" header)

To be more specific, right now I'm having a problem with the separator on decimal numbers (comma vs dot): If the wrong separator comes in a url parameter the webservice returns 404. But I'd like to find a proper solution that will handle the JSON reading/writing as well.
Note:
I've tried the answer in this other question but it references classes I don't have in my Jersey jars (If, QuantityXmlAdapter and NumberPersonalizedXmlAdapter) and I can't find a clue about them on Google.
I read your questions as this.
Your method
#GET
#Path('/resource/{decimal}')
public Response getResoureWithDecimal(#PathParam("decimal") double decimal)
Your request
GET /resource/1,2
Clearly, 1,2 is not a Java double and JAX-RS has no way to convert it to one.
Possible solution
Use a String as the parameter type and convert it with your own converter.
#GET
#Path('/resource/{decimal}')
public Response getResoureWithDecimal(#PathParam("decimal") String decimal) {
double decimalAsDouble = convertStringWithCommaToDouble(decimal);
// ...
}
The implementation of convertStringWithCommaToDouble(String) is up to you.

Resources