Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-11421

Inherit value for @RequestMapping if not overridden

    XMLWordPrintable

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Won't Fix
    • 4.0.1
    • None
    • Web
    • true

    Description

      If a @RequestMapping path is defined on a superclass, the path is inherited by subclasses (e.g., the {{@RequestMapping}}s on their methods are relative to the superclass's base path).

      I'm using the Spring content negotiation feature to serve both HTML views and a JSON API from the same URLs, based on the client's Accept header. In order to overload the URL, I have to differentiate the implementation methods by having different produces attributes; I specify "application/json" in the @RequestMapping for the JSON subclass.

      The value attribute on @RequestMapping is not required, but if a subclass is annotated, Spring overrides the path from the parent with an empty string and attaches the subclass's controllers to the root path.

      In order to avoid having to duplicate @RequestMapping data on each class, I suggest changing the resolver to treat a missing value attribute in a @RequestMapping as if the annotation were not there, rather than implicitly treating it as an empty string. The behavior of a @RequestMapping on a subclass without a value attribute is not specified by the docs, so this wouldn't break the existing contract. In the alternative, I suggest making value a required attribute to eliminate this pitfall.

      Attachments

        1. AbstractController.java
          0.4 kB
        2. HtmlController.java
          0.7 kB
        3. JsonController.java
          0.5 kB

        Activity

          People

            rstoya05-aop Rossen Stoyanchev
            w_c_smith Christopher Smith
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              3 years, 47 weeks, 2 days ago