Hard-coded @RequestMapping URL in Spring MVC Controller

I am learning Spring 3 and I use it in a simple web application.

Now I am implementing Spring MVC controller using annotations, and I am wondering: Is there any best practice using @RequestMapping annotation?

I mean: I saw that usually the URL displayed in this annotation is hard-coded in the class ...
Is there a way to pass the url in a โ€œloosely coupled wayโ€ (to get a more reusable class)?

I know that there are some wild cards that can be used, but I think this is not a solution ... Am I wrong?

EDIT:

I am adding an example to better explain my doubts.

Suppose I want my controller to be called by a request to /foo/bar/baz/mypage.htm , in my controller the handler method will be annotated using @RequestMapping("/foo/bar/baz/mypage") .

Now I decided to change the URL that starts my controller to /foo/bar/otherpage.htm , so I need to edit my class, put @RequestMapping("/foo/bar/otherpage") in my handler method, recompile the project and deploy it again.

It seems to me that this is not so practical ...

+6
source share
3 answers

Currently annotated controllers are not very customizable.

As far as I know, the only possible approach to this problem is to use alternative HandlerMapping s to configure the "base URLs" of the controllers. For example, as follows:

 // Note the absense of @Controller to prevent this controller // from being discovered by DefaultAnnotationHandlerMapping public class FooController { @RequestMapping("/list") public String list(...) { ... } @ReqeustMapping("/save") public String save(...) { ... } } 

.

 <bean class = "org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping" /> <bean name = "/foo" class = "FooController" /> <bean name = "/bar" class = "FooController" /> 

In this example, two instances of FooController process /foo/list , /foo/save , /bar/list and /bar/save respectively.

The upcoming Spring 3.1 will have an improved Spring 3.1 architecture ( Spring 3.1 M2: Spring MVC Enhancements ) which seems more flexible, although I haven't tested it yet.

+5
source

I think you are trying to solve the wrong problem. If you want to change the pages corresponding to the controller, you need to change the file somewhere. Would you rather change the file with the appropriate code under it, or better work with some XML files that specify the URL and class, and then you have to worry about the file being in the right place at runtime?

As there should be almost no code in your controller , you should think of your controllers as compiled configuration files. Also, if you are using a build system like Maven or Ant and not compiling individual files manually using Javac, then compilation time should not be a problem. If it becomes one, perhaps it is time to divide the project into subprojects.

I think you should just accept this and see that this is probably not the problem you are thinking about. Also, did you know that controllers can match expressions, not just literal strings? This gives you some flexibility in your name.

If you really want it, you can just go back to Spring 2.0 XML configuration style , but I do not think that anyone would recommend it.

+1
source

I think this is not the best practice, but have you tried with @PathVariable annotations?

  @RequestMapping(value="/path/{word}", method=RequestMethod.GET) public ModelAndView myRestMethod(@PathVariable String word) { ... } 
0
source

Source: https://habr.com/ru/post/902825/


All Articles