Skip to content

Allow controller parameter annotations (@RequestMapping, @PathVariable...) to be read from interfaces or parent classes #7730

Closed

Description

@karaatanassov

We are trying to use external REST API definition that would generate interfaces one implements in Java. For the time being we try to use Swagger.

Unfortunately Spring does not allow us to separate the API definition from implementation as the parameter annotations on controllers are not read from interfaces. Instead of that Spring always looks into the concrete implementation whose purpose is to implement a contract not define it. Weirdly enough the @RequestMapping annotation can be declared on the interface and is picked up by the implementing controller. It would be great if similar functionality is provided for @RequestBody, @PathVariable, @RequestHeader etc. that are defined on the individual parameters.

It seems that a potential fix has to update/extend the implementation of

Annotation[] org.springframework.core.MethodParameter.getParameterAnnotations()

this is called by

MethodParameter[] org.springframework.web.method.HandlerMethod.initMethodParameters()

called by the
org.springframework.web.method.HandlerMethod constructor which in turn is called by

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.createHandlerMethod(Object, Method).

There is a sample but hacky work around found on StackOverflow. See

http://stackoverflow.com/questions/8002514/spring-mvc-annotated-controller-interface/8005644#8005644

It does require some creativity to put in practical use with Spring Boot i.e. WebMvcRegistrations

So the basic ask is to pick up parameter annotations for controller from implemented interfaces e.g.

public interface MyControllerInterface {

    @RequestMapping(path="/test", method=RequestMethod.POST)
    public ResponseEntity<String> echo(@RequestBody String input);

}

@RestController
public class MyControllerImpl implements MyControllerInterface {
    @Override
    public ResponseEntity<String> echo(String input) {
        return new ResponseEntity<>(input, HttpStatus.OK);
    }
}

It may make sense to to use @RestController or alike annotation on the interfaces whose methods will be checked.

Activity

  1. added
    status: waiting-for-triage
    on Dec 22, 2016
  2. philwebb commented on Dec 22, 2016

    @philwebb
    Member

    @karaatanassov Thanks for the suggestion but this is something that the core Spring MVC team would need to consider. You can raise an issue on their tracker here.

  3. added
    status: invalid
    and removed
    status: waiting-for-triage
    on Dec 22, 2016
  4. bclozel commented on Dec 27, 2016

    @bclozel
    Member

    For reference: SPR-15046 was raised, which is a duplicate of SPR-11055.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions