1

ASP.Net MVC contains useful class UrlHelper, and using that I can create URL to my method controller like this:

var myUrl = helper.Generate<MySuperController>(x => x.MyAwesomeMethod(
     myCharmingParameter1, myPrettyParameter2));

where helper is UrlHelper instance. When I rename MyAwesomeMethod with ReSharper, it will be renamed everywhere and myUrl will points to new location automatically.

I need similar functionality for use in Spring MVC: for example, when I change value in @GetMapping("/api/getFields") annotation on method. I don't want to replace to new url everywhere in the project. I just want to generate new url automatically.

How it can be reached?

Alex T
  • 2,067
  • 4
  • 20
  • 27

1 Answers1

0

A fast solution would be to use params from property files or define a custom Property Configuration class(apply custom namings during start up, check PropertySourcesPlaceholderConfigurer) which injects the values in the controllers. F.E you could define it as :

@RequestMapping("${links.link1}")

And in the property file

links.link1=/my/link

A similar answer is here

AntJavaDev
  • 1,204
  • 1
  • 18
  • 24
  • It is not exactly what I want. What happens if somebody change from links.link1 to links.mySuperPuperlink? Right, urls will be broken, if they are not fixed manually. And it became known only in runtime, only if this link somebody tries to click. May be it will be found by unit test, but what if it is not covered? In ASP.Net MVC it will be found when compiling app, or fixed automatically without any error. – Alex T Oct 24 '19 at 10:06
  • really lost you here, how does ASP realizes that a URL is broken during compile time ???? its just a binding param, so if you have misplaced the url bindings, still youll get to know it when u click it.....? Am i missing something? – AntJavaDev Oct 24 '19 at 10:10
  • ASP.Net use reflection to generate url. Url helper has generic parameter for controller type, and lambda for method. If you rename method (without ReSharper) and not fix lambda when you generate url, compilation error occurs: method not found. If you use ReSharper to rename method, method name will be replaced everywhere, so it will be just work. Now you see what I mean? – Alex T Oct 24 '19 at 10:18
  • hmm ok , now i got what you mean, which is a bit different on how Spring instantiates the controllers. Take a look [here](https://stackoverflow.com/questions/5758504/is-it-possible-to-dynamically-set-requestmappings-in-spring-mvc) , might be helpful. – AntJavaDev Oct 24 '19 at 14:42