What's the supported URL schema of SprinMVC `getServletMapping()`? - spring-mvc

According to the JavaDoc for getServletMapping():
Specify the servlet mapping(s) for the DispatcherServlet, e.g. '/',
'/app', etc.
According to SpringMVC online reference doc, there are 2 ways to specify a URL for #RequestMapping annotation:
URI templates
Ant-style path patterns
So, does these 2 options apply to getServletMapping() as well? Which one is supported by getServletMapping()? Or both?
Ref:
Ant path patterns: Ant path style patterns
URI template: https://www.rfc-editor.org/rfc/rfc6570

Related

Spring Boot Filter URL Mapping

The request mapping for my controller is something like this:
/hospital/{hospitalId}/department/{departmentId}/doctors
And i tried to add the pattern for authentication required filter:
/hospital/*/department/*/doctors
But it's not working. It's there a chance to make this work?
The mapping for a filter isn't a Ant-style mapping as you're used to in Spring, but a mapping as defined in the Servlet specification. In section 12.2 it says:
the following syntax is used to define mappings:
A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
A string beginning with a ‘*.’ prefix is used as an extension mapping.
The empty string ("") is a special URL pattern that exactly maps to the
application's context root, i.e., requests of the form http://host:port//. In this case the path info is ’/’ and the servlet path and context path is empty string (““).
A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
All other strings are used for exact matches only.
/hospital/*/department/*/doctors only meets the criteria of the final bullet so it's treated as an exact match.
The best that you can do within confines of the servlet specification is to use /hospital/* and then do some secondary matching in your filter's code. You could use Spring Framework's org.springframework.util.AntPathMatcher to do so.

openapi documentation how to handle extension in the path

I have URLs like:
/return/{pid}.xml?d1='123'&d2='345'
/return/{pid}.json?d1='123'&d2='345'
the swagger specification calls for:
path:/return/{pid}
....
but how can I map the extension ie
path:/return/{pid}.xml
or
path:/return/{pid}.json
It is a jersey+spring mvc application - so both the URLs are hitting the same controller and only based on the extension the rest framework is going to generate the xml / json output.
I can't ignore the extension from the path ie:
path:/return/{pid}
because the user needs to know that he/she has to provide the file extension as part of the URL. Also I can't use two paths corresponding to xml / json because they are treated the same by the application. In addition it will duplicate things (I am not sure whether there is a fall-through like mechanism just like "case" statements in c++/java "switch" block)
In Swagger specs,You can define the file extensions in the path as below :
/return/{pId}.{fileExtension}
and define the fileExtension in parameters .
also the below is valid (not for your case) :
/return/pid.{fileExtension}

System.Web.Http.Routing.UrlHelper creates a link with querystrings in Web API 2

When using WebAPI 2 and Attribute Routing, a call to UrlHelper.Link returns a route url with querystring attributes. The same code when using convention based routing returns the route wi the slashes as expected.
For example -
Attribute Routing: http://a.domain.com/api/foods?foodid=1
Convention Routing: http://a.domain.com/api/foods/1
Anyone know how to tell the UrlHelper to not use query strings when using attribute routing?
I am trying to convert some sample code from a pluralsight video into Web API 2.
You can get the code I am using here:
https://github.com/PriceIsByte/WebAPI/tree/issue/1/attribute_routing
Maybe you find the answer already. Here is mine though.
You must have an optional parameter in your route and also add a name to it. In your case it would be something like this
[Route("{foodid?}", Name="Foo")]
That will fix it.

Hash escape in URL (routing.yml/path) Symfony 2

I have problem with urls in my Symfony 2.3 aplication.
I have defined routing like this:
home_how_to_buy:
path: /strefa-wiedzy#jak-kupic
defaults:
_controller: FrameworkBundle:Template:template
template: 'GLHomeBundle:Default:faq.html.twig'
The problem is that when I create links for this page I have something like:
app_dev.php/strefa-wiedzy%23jak-kupic
I have been looking for escaping in yml files, but none of those solutions work for my path.
I will be gratefull for any help.
As stated in my answer here the hashtag is not intended to be in symfony routing. You can do the suggested workaround. But first you should consider, do you really need url-fragments in routing?
PHP's rawurlencode() encodes all chars except a-zA-Z0-9-._~ according to RFC 3986. But we want to allow some chars to be used in their literal form (reasons below). Other chars inside the path must of course be encoded, e.g. ? and # (would be interpreted wrongly as query and fragment identifier), ' and " (are used as delimiters in HTML).

File path as MVC route argument

Part of my application maps resources stored in a number of locations onto web URLs like this:
http://servername/files/path/to/my/resource/
The resources location is modelled after file paths and as a result there can be an unlimited level of nesting. Is it possible to construct an MVC route that matches this so that I get the path in its entirety passed into my controller? Either as a single string or possibly as an params style array of strings.
I guess this requires a match on the files keyword, followed by some sort of wildcard. Though I have no idea if MVC supports this.
A route like
"Files/{*path}"
will get the path as a single string. The * designates it as a wildcard mapping and it will consume the whole URL after "Files/".
For more information on ASP.NET's Routing feature, please see MSDN:
http://msdn.microsoft.com/en-us/library/cc668201.aspx
And for the "catch-all" parameters you want to use, see the section under "Handling a Variable Number of Segments".

Resources