1

Posted in spring forum with no response.

I have the following code snippet (from here), which is part of my pet project.

@Controller
@RequestMapping("/browse")
public class MediaBrowser {
   ...

    @RequestMapping("/**")
    public final ModelAndView listContents(final HttpServletRequest request) {

        String folder = (String) request.getAttribute(
                HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
                ...
    }

I access the following url:

http://localhost:8080/myapp/browse

  • In spring 3.0.6.RELEASE, I got the folder variable as null, which is the expected value.
  • In spring 3.1.RC1, the folder variable is /browse.

Is this a bug or has something changed in spring-3.1?

Raghuram
  • 51,854
  • 11
  • 110
  • 122

2 Answers2

4

As skaffman said, you probably shouldn't use PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE. Take a look at How to match a Spring @RequestMapping having a @pathVariable containing "/"? for an example of using AntPathMatcher to accomplish what you are trying

Community
  • 1
  • 1
rhollencamp
  • 533
  • 4
  • 13
  • Like `PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE`, the suggested alternate `BEST_MATCHING_PATTERN_ATTRIBUTE` is also an internal framework internal implementation details, which is not expected to be relied on. However, it works and so I am accepting the answer. – Raghuram Apr 26 '12 at 06:53
3

This looks very much like an internal implementation detail of the framework, one that you should not be relying on.

The javadoc for PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE says:

Note: This attribute is not required to be supported by all HandlerMapping implementations. URL-based HandlerMappings will typically support it, but handlers should not necessarily expect this request attribute to be present in all scenarios.

I wouldn't be surprised if the behaviour changed slightly between 3.0 and 3.1.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Thanks. I have seen the javadoc and realize it is unreliable. Perhaps someone can suggest an alternate solution - essentially a way to specify folder names in REST-style URL. Got this one from http://stackoverflow.com/questions/3686808/spring-3-requestmapping-get-path-value – Raghuram Nov 04 '11 at 08:52