3

So I'm currently developing a platform for my work using Spring MVC 3.2, with an XML configuration, and what I'm looking for is some syntactic sugar.

Basically we have numerous controllers, and since I've been the one who organized all the packages, I packed them in the same method I've gotten used to in Angular JS (i.e related concepts with related).

So now I have a package structure of:

  • com.company.general
    • com.company.general.security
    • com.company.general.accounts
  • com.company.reports
    • com.company.reports.someReport
    • com.company.reports.anotherReport

Now the big issue I'm personally having (it's more I'm looking for an easy way to do this) is trying set a standard path for a specific package.

After reading the above I realize it is not so clear, so I'll try and clarify.

Right now, each of my controllers under the reports package, have:

@Controller
@RequestMapping("/reports/reportName")

Now the only thing that changes in that chunk of code is the reportName for different controllers. I'm unsure how to, if it is at all possible, to have it so I can specify, all controllers under package com.company.reports has a default "/reports/" path, and then when I specify the @RequestMapping in the controller it simply appends to this.

I thought this would be something doable through when I setup the since it seemed logical with it doing the assignment (or what I believe the assignment of paths) but to no avail.

Any help would be much appreciated, many thanks

  • There is an open enhancement request to implement this feature. See [SPR-15913 Handle @RequestMapping at package level](https://jira.spring.io/browse/SPR-15913), which was superseded by [SPR-16336 Ability to provide an external base path for controllers](https://jira.spring.io/browse/SPR-16336). – Daniel Trebbien Apr 22 '18 at 16:00

1 Answers1

0

You can, maybe, try with an abstract class:

@RequestMapping(value = "/reports")
public abstract class AbstractReportController {
    ...
}


public class MyReport extends AbstractReportController  {
    @RequestMapping(value = "/myReportName")
    public String aMethod(Model model) {
        ...
    }
 }

public class AnotherReport extends AbstractReportController  {
    @RequestMapping(value = "/anotherReportName")
    public String aMethod(Model model) {
        ...
    }
 }

This work.

perbellinio
  • 682
  • 5
  • 14
  • Nope won't work, sorry but Spring doesn't do inheritance with Request mapping like that, check [here](http://stackoverflow.com/questions/5268643/spring-mvc-requestmapping-inheritance) this gives a pretty good example and explination – Shadowmancer May 21 '16 at 00:51