I have URL #RequestMapping(value = {"/sessionkey"}, method = {RequestMethod.PUT}, produces = {"application/json"}) in my spring rest API. but it accepts special character, like "/sessionkey#?".
I want my API to accept only exact match the "/sessionkey" URL and should not accept any special character.
Related
I am working on an example project to understand better endpoint matchers::
#GetMapping(path ="/v3**", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<byte[]> genericV3GetRequestProxy(ProxyExchange<byte[]> proxy);
But when I send this http call it is not matching:
http://localhost:9000/api/v3asdasd/asd
Instead if I send this one it works:
http://localhost:9000/api/v3asdasd
In the end the correct solution is just:
#GetMapping(path ="/v3/**", produces = MediaType.APPLICATION_JSON_VALUE)
But I wanted to understand why it behaves like I showed.
When you added a slash to your URI you created a separate path segment, as / is defined as the path separator per RFC 3986.
https://www.rfc-editor.org/rfc/rfc3986#section-3.3
In Jersey Rest API
if any common request parameters are there then we can capture that value at RootResource level using the below code.
#QueryParam("q")
private String qQueryParams
Is there any similar approach in Spring Rest API.
In other words, all my endpoint URL will contain the query parameter "q". How to capture this data at class level instead of every request.
Thanks, Vijay
you can use #RequestMapping({q}/test) above controller and pass #PathVariable String q as method argument.
#Controller
#RequestMapping(value = "{q}/test")
class TestController {
#RequestMapping(value="/abc")
public ModelAndView doSomething(#PathVariable String q) {
// do something with q...
}
}
In my Spring Boot application I have an URL concatenated from values that go from client side, for example:
/api/foo/{client-defined-value}/bar/
and the real URL can be something like this:
/api/foo/OBCH.%20Z%C3%81STUPCI/bar/
(not url encoded value is "OBCH. ZÁSTUPCI")
In a controller I have definition of GET request mapping:
#GetMapping(value = "/foo/{value:[^\\/]+}/bar/")
but the mapping is not found:
No mapping found for HTTP request with URI...
What am I doing wrong?
You can acces the URL variable by using the annotation #Pathvariable("client-defined-value") as a method argument.
Like so:
#RequestMapping(value = "/api/foo/{client-defined-value}/bar/")
public void foo(#PathVariable("client-defined-value") String value) {
doSomething…
}
I'm trying to migrate code from Jax-Rs (jersey implementation) to a Spring MVC entry point:
jax-rs:
#GET
#Path("{bundle}/bundle{min: (-min)*}.{extension: js|css}")
public Response getBundle(#PathParam("bundle") String bundle, #PathParam("min") String min, #PathParam("extension") String extension)
Spring MVC:
#RequestMapping(method = GET, path = "{bundle}/bundle{min:(-min)?}{extension:\\.(js|css)?}")
public void getBundle(#PathVariable String bundle, #PathVariable String min, #PathVariable String extension)
According to Spring MVC documentation, i can use regex for #RequestMapping. The syntax is similar to jaxrs but the entrypoint doesn't work (404 Not Found).
Example of value for the entrypoint: http://localhost:8080/foo/bundle-min.css
I've found a solution with #RequestMapping(method = GET, path = "{bundle}/bundle**") but i have to parse the string to catch my needed parameter values.
Your regex seems fine but replace those capturing groups with non-capturing groups, like following:
{bundle}/bundle{min:(?:-min)?}{extension:\.(?:js|css)?}
With this regex, if you fire a request to foo/bundle-min.css, the bundle would be foo, the min would be -min and the extension would be .css.
With spring rest is there any reason to use request param?
For a search i don't know if i shoud use
#RequestMapping(value = "/lodgers/{searchParam}", method = RequestMethod.GET)
public Lodger getAllLogders(#PathVariable("searchParam") String searchParam)
or
#RequestMapping(value = "/lodgers/", method = RequestMethod.GET)
public Lodger getAllLogders(String searchParam)
As I use it, a path (pathVariables) points to a resource.
A queryParam (requestParam) results in a subset of a resource.
If you want certain users from /Users (e.g. beginning with "A", or lodgers named "Curt") this would be a subset, of all /Users and I see not a very good reason for having a special resource with that criteria, so I would use a queryParam here.