13

Can I do the following in Spring MVC

Suppose I have the Base GenericController as follows with one request mapping "/list"

@Controller
public class GenericController<T>{

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public @ResponseBody List<T> getMyPage(){
        // returns list of T
    }

}

Below are my two controllers

@Controller(value = "/page1")
public class Page1Controller extends GenericController<Page1>{

}

@Controller(value = "/page2")
public class Page2Controller extends GenericController<Page2>{

}

Now will i be able to access the url "/page1/list" and "/page2/list" where first goes to Page1Controller and second goes to Page2Controller.

zdesam
  • 2,936
  • 3
  • 25
  • 32

2 Answers2

14

That's not possible and was already rejected, see SPR-10089. I think this would be somewhat confusing and in addition, it is very unlikely that those method behave exactly the same besides the different mapping. But you can use delegation instead:

public class BaseController<T> {
    public List<T> getPageList(){
        // returns list of T
    }
}

@Controller(value = "/page1")
public class Page1Controller extends BaseController<Page1>{
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public @ResponseBody List<Page1> getMyPage() {
      return super.getPageList();
    }    
}

@Controller(value = "/page2")
public class Page2Controller extends BaseController<Page2>{
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public @ResponseBody List<Page2> getMyPage() {
      return super.getPageList();
    }
}
Robin
  • 3,226
  • 2
  • 20
  • 27
  • 1
    I think SPR-10089 does not fit this example, which should conceptually work. SPR-10089 will not "stack" url mappings on @RequestMapping on the class level. see this http://stackoverflow.com/questions/5268643/spring-mvc-requestmapping-inheritance for a complete explanation. – Nitzan Volman Oct 01 '14 at 18:31
  • In this example, should be ok to have a class level annotation, and method level annotations in the base class. – Nitzan Volman Oct 01 '14 at 18:33
  • how would that work? for each of the resources you usually have a CrudRepository or a similar Service.. by delegating to the parent how would you get the correct repository? You will need to pass that as an argument somehow, which would be ugly – Michail Michailidis Oct 15 '17 at 09:27
  • @robin It seems that this answer is out of date. Take a look at manish's answer. – Dariusz Oct 28 '20 at 11:21
8

For those looking for something similar with Spring Framework 4.x, the class hierarchy provided by the OP is possible. A sample application is available on Github. It allows users to view a list of books or a list of magazines as JSON.

manish
  • 19,695
  • 5
  • 67
  • 91
  • Why do you say that the super type does not have the `@RequestMapping` annotation? The super type `BaseController` does have the annotation on the `list` method which is what the first code snippet in the question shows. May be you have a different requirement and got confused? – manish Feb 22 '16 at 15:44
  • manish, you're right, sorry for wrong comment - I'll delete it and upvote ;-) Edit: The solution works fine for me, so IMO this is the correct answer. – Michael Wyraz Feb 23 '16 at 19:23