I have to support following URL format
/service/country/city/addr1/addr2/xyz.atom
/service/country/city/addr1/addr2/addr3/xyz.atom
where country
and city
can be mapped to @PathVariable
but after that the path can be dynamic with multiple slashes. The end part will have .atom
or similar.
I tried following, but none of the options seem to be working
Wildcard
@RequestMapping(value="/service/{country}/{city}/**")
Regex
@RequestMapping(value="/service/{country}/{city}/{addr:.+}")
UseSuffixPatternMatch
Override method in Config class@Override public void configurePathMatch(PathMatchConfigurer configurer) { configurer.setUseSuffixPatternMatch(false); }
Looks like combination of slash and dots don't work with above solutions. I keep getting 406
for non-matching Accept header, or 404
2 Answers
Answers 1
Can you try this,
@RequestMapping(value="/service/{country}/{city}/{addr:[a-z]+\\\\.(atom|otherExtensions)}")
Just have to specify the complete regex format wherein you are expecting an extension at the end of the url such as atom, since this will be interpreted by default as MediaType by Spring.
another solution is specify the accepted MediaTypes
@RequestMapping(value="/service/{country}/{city}/{addr}", consumes = {MediaType.ATOM, MediaType...})
You can create custom MediaTypes if it is not predefined in Spring.
Answers 2
The most dynamic approach would be to use MatrixVariable
to manage the list of addresses but it is not applicable in your context since the paths cannot be modified as far as I understand from your question.
The best thing you can do to manage your dynamic path is to proceed in two steps:
- Set a
RequestMapping
that extracts all the data except the addresses - Extract the addresses manually in the method
So for the first step you will have something like that:
@RequestMapping(value="/service/{country}/{city}/**/{file}.atom") public String service(@PathVariable String country, @PathVariable String city, @PathVariable String file, HttpServletRequest request, Model model) {
This mapping matchs with all the required paths and allows to extract the country, the city and the file name.
In the second step we will use what has been extracted to get the addresses by doing something like this:
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); path = path.substring(String.format("/service/%s/%s/", country, city).length(), path.length() - String.format("%s.atom", file).length()); String[] addrs = path.split("/");
- First we extract from the request the full path
- Then we remove what we have already extracted which are here the beginning and the end of the path
- Then finally we use
String.split
to extract all the addresses
At this level you have everything you need.
0 comments:
Post a Comment