Skip to content

Enable REST controller method parameter annotations on an interface [SPR-11055] #15682

Closed

Description

@spring-projects-issues

Paweł Mendelski opened SPR-11055 and commented

Please, enable inheritance of controller related annotations from implemented interfaces.

I would like to share rest service interface between client and server. This way I could provide a really convenient mechanism - client-proxy, like the one RestEasy provides.

Shared interface:

@RequestMapping("/random")
public interface RandomDataController {

	@RequestMapping(value = "/{type}", method = RequestMethod.GET)
	@ResponseBody
	RandomData getRandomData(
			@PathVariable(value = "type") RandomDataType type, @RequestParam(value = "size", required = false, defaultValue = "10") int size);
}

Server implementation:

@Controller
public class RandomDataImpl implements RandomDataController {

	@Autowired
	private RandomGenerator randomGenerator;

	@Override
	public RandomData getPathParamRandomData(RandomDataType type, int size) {
		return randomGenerator.generateRandomData(type, size);
	}
}

Client code:

// ...
RandomDataController randomDataController = SpringMvcProxyFactory.create(RandomDataController.class, "http://localhost:8080");
RandomData rd = randomDataController.getRandomData(RandomDataType.ALPHA, 100);
// ...

At the moment I cannot write SpringMvcProxyFactory because parameter annotations from interface RandomDataController are not inherited by RandomDataControllerImpl.


Affects: 4.3.3

Issue Links:

Referenced from: commits 790d515, 1f5d0fa

30 votes, 37 watchers

Activity

  1. spring-projects-issues commented on Aug 24, 2014

    @spring-projects-issues
    CollaboratorAuthor

    George Georgovassilis commented

    There is an implementation here: https://github.com/ggeorgovassilis/spring-rest-invoker
    I would be happy to contribute it as a module or to spring-web if there is interest

  2. spring-projects-issues commented on Aug 26, 2014

    @spring-projects-issues
    CollaboratorAuthor

    George Georgovassilis commented

    Upon re-reading the ticket's description, the submitter asks for a common interface that would work both for controllers that expose REST APIs and clients that consume them. The implementation above generally requires interfaces that might not make sense when implemented by a controller.

  3. spring-projects-issues commented on Aug 26, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Controller annotations can be inherited from a base class or interface. Method parameter annotations however cannot be. They must be directly on the controller method in all cases. This ticket is a request to change that.

    Indeed your proposal has similar purpose but does not depend on this ticket since in your case the interfaces are only used for generating a client proxy.

    That said once the ability to create a client proxy from an interface with the same annotations is available, it becomes a natural question: should it be possible for a controller and a client proxy to share an interface?

  4. spring-projects-issues commented on Aug 28, 2014

    @spring-projects-issues
    CollaboratorAuthor

    George Georgovassilis commented

    In that case I'd like to decouple my implementation from the concrete decision whether a single or multiple interfaces are to be used. New ticket #16747.

  5. spring-projects-issues commented on Jan 26, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Matt Benson commented

    This issue also applies to parent classes.

  6. spring-projects-issues commented on Feb 23, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Matt Benson commented

    PR submitted: #976

  7. spring-projects-issues commented on Feb 24, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Wow this is a very extensive effort and a perfect example of the sort of thing that you could discuss (beforehand) on our contributor's mailing list.

    Style and programming model issues aside, there is a whole separate discussion if we should even re-use the same annotations for client-proxy generation or create more dedicated annotations for the purpose (see #16827). Alternatively with what we have today wouldn't it be easy to generate interfaces on the fly from a controller's methods and annotations? That way method parameter annotations can remain close to the class as promoted by the programming model today.

  8. spring-projects-issues commented on Feb 24, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Matt Benson commented

    TBH I decided to take my chances rather than subject my inbox to the burden of another mailing list. With regard to client proxies, spring-cloud is already doing this with their support for Netflix Feign, and until recently their documentation (at least somewhat erroneously) reported that it was possible to use a Spring-annotated REST interface for client and server alike. The non-inherited nature of parameter annotations is in fact the missing ingredient here. If people are going to use interfaces for client proxies, I can't see how forcing them to define their REST API in multiple places does anything other than expose them to the risk that these definitions will fall out of sync.

    My interpretation of "on the fly" is "at runtime." I don't see how that helps clients who want to consume the hypothetical generated interface. Certainly it would be possible to generate such an interface, but it seems like this would be more a tooling step or, at best, an annotation processor, and getting these generated types into a consumable client artifact would be quite a build-system-dependent task. It is not at all clear to me that this solution would be better than a basic, Java-based one.

  9. spring-projects-issues commented on Apr 12, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Matt Benson commented

    With the recent closure of the development forum, do any other Spring developers have any thoughts on the proposed fix?

  10. spring-projects-issues commented on Sep 26, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    If we did implement this feature to allow method parameter annotations on supertypes it would most likely be exposed through AnnotationUtils which we currently use to find method and type level annotations on supertypes (see for example RequestMappingHandlerMapping). The various argument resolver implementations would delegate to AnnotationUtils unlike the PR which introduces the same as base class functionality. Also the current algorithm in AnnotationUtils for method and type level annotations is also a lot more involved and so would be the one for method parameters. This is not really meant as criticism but just saying that the problem is more complex. We are however taking a look at this still. There are several related tickets so I'm going to assign this one also to Juergen Hoeller for consideration.

    From a Spring MVC perspective I think basic support for the Spring Cloud with Netflix Feign use case (i.e. allow the same interface to be used on client and server side)is fine as a use case but allowing a method parameter to be anywhere is another matter. It means for a controller method with several arguments the annotations for each argument could literally be in a different superclass. I'm not sure it should be so wide open. Some meaningful constraints focusing on the target use cases we want to support should perferably be in order.

  11. spring-projects-issues commented on Oct 14, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Alexandre Navarro commented

    +1

  12. spring-projects-issues commented on Oct 10, 2017

    @spring-projects-issues
    CollaboratorAuthor

    Elias O commented

    One more use case for method param annotation inheritance:
    I'm using Swagger Codegen to generate API interfaces from OpenAPI specs during build process so that I can implement them directly in my code. Those generated interfaces contain all Spring MVC related annotations so I don't need to care about annotating my methods. Except method params. I had to copy them once again because Spring doesn't see them.

  13. spring-projects-issues commented on Oct 30, 2017

    @spring-projects-issues
    CollaboratorAuthor

    Yura Nosenko commented

    @Elias O, have a similar story with RAML. It's a huge gap, since the method-level annotations work as expected. This feature is a MUST!

  14. spring-projects-issues commented on Oct 30, 2017

    @spring-projects-issues
    CollaboratorAuthor

    Yura Nosenko commented

    When it comes to validation, there is a workaround - @Validated. It will actually make jsr303 param annotations inheritable.

  15. spring-projects-issues commented on Oct 30, 2017

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Do you mean that Swagger can't generate a class for a server stub, and same for RAML?

  16. 6 remaining items

  17. spring-projects-issues commented on Jul 18, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Damian Sima commented

    you rock Juergen Hoeller

  18. added
    type: enhancement
    has: votes-jira
    in: web
    on Jan 11, 2019
  19. 448045565 commented on Mar 26, 2019

    @448045565

    @jhoeller hi,how can I achieve the same effect in spring5.0.x?

  20. added 4 commits that reference this issue on Feb 7, 2020
  21. added 2 commits that reference this issue on Feb 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

has: votes-jirain: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions