14

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.

1
  • 3
    AFAIK, annotations are not inherited, so this may not work as expected Commented Mar 13, 2013 at 10:41

2 Answers 2

15

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();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

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 stackoverflow.com/questions/5268643/… for a complete explanation.
In this example, should be ok to have a class level annotation, and method level annotations in the base class.
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
@robin It seems that this answer is out of date. Take a look at manish's answer.
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.

2 Comments

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, 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.