Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequestMapping with slashes and dot

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

like image 763
sidgate Avatar asked Mar 14 '16 14:03

sidgate


People also ask

Can PATH variable contain slash?

Unfortunately, we soon find out that this returns a 404 if the PathVariable contains a slash. The slash character is the URI standard path delimiter, and all that goes after it counts as a new level in the path hierarchy. As expected, Spring follows this standard.

What is the @RequestMapping annotation?

annotation. RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

Which annotation is used with @RequestMapping?

The @RequestParam annotation is used with @RequestMapping to bind a web request parameter to the parameter of the handler method. The @RequestParam annotation can be used with or without a value.

What does @RequestMapping do in spring?

One of the most important annotations in spring is the @RequestMapping Annotation which is used to map HTTP requests to handler methods of MVC and REST controllers. In Spring MVC applications, the DispatcherServlet (Front Controller) is responsible for routing incoming HTTP requests to handler methods of controllers.


2 Answers

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:

  1. Set a RequestMapping that extracts all the data except the addresses
  2. 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("/");
  1. First we extract from the request the full path
  2. Then we remove what we have already extracted which are here the beginning and the end of the path
  3. Then finally we use String.split to extract all the addresses

At this level you have everything you need.

like image 71
Nicolas Filotto Avatar answered Oct 04 '22 00:10

Nicolas Filotto


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.

like image 32
vine Avatar answered Oct 04 '22 00:10

vine