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

Inherit value for @RequestMapping if not overridden

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Minor
    • Resolution: Won't Fix
    • Affects Version/s: 4.0.1
    • Fix Version/s: None
    • Component/s: Web
    • Last commented by a User:
      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

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

              Dates

              Created:
              Updated:
              Resolved:
              Days since last comment:
              3 years, 30 weeks, 1 day ago