Add a prefix to the URLs of all controller classes in the package

I am developing a RESTful service using Spring 3.0 running on Tomcat Apache 6.0. The DispatcherServlet Spring dispatcher is configured to "/ spring / *". The Spring servlet handles multiple clients and has many controllers in the application. I am adding a new controller for the REST service. I want all my controller classes to have an identifier prefix like "ws" (web service) so that the entire URL of the resource looks like this: http://<server>:<port>/<context>/spring/ws/service1

The only way I found with Spring annotation is to use @RequestMapping as follows:

 @Controller @RequestMapping("/ws/service1") public class Service1 { @RequestMapping(method=RequestMethod.GET) @ResponseBody public String getHtml() { ... } .... } 

But since I have dozens of classes, I don’t want to put the prefix β€œ/ ws” in each class. So, if another developer adds a new service, he should not forget to put this prefix, and also if we decided to change the prefix name from "/ ws" to something else, I do not need to change all the files. I found that the @RequestMapping annotation applies only to methods or classes, not to the package level.

Is there any way I can configure so that all my REST controllers are accessible with a prefix?

Please note that I cannot change the URL of the web.xml URL for the Spring servlet, as there are other controllers that work with this URL.

+7
source share
2 answers

You might want to look at the Spring 3 support configuration agreement , in particular ControllerClassNameHandlerMapping . You are not actually locating the URL in @RequestMapping , but it is determined by the position of the handler package.

If you want the display URLs to display the name of the controller package, you must set the basePackage ControllerClassNameHandlerMapping property. documentation says

Install the base package that will be used to create the path mappings, including all subpackages under these packages as path elements. The default value is null, using the short class name for the generated path, with no controller package present in the path. Specify the base package, for example "com.mycompany.myapp", to include subpackages in this base package as path elements, for example. generating the path "/ mymodule / buyform" for the class name "com.mycompany.myapp.mymodule.BuyForm". Subpacket hierarchies are represented as separate path elements, for example. "/ mymodule / mysubmodule / buyform" for the class name "com.mycompany.myapp.mymodule.mysubmodule.BuyForm".

So an example beans might be

 <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> <property name="basePackage" value="com.company.project"/> </bean> <context:component-scan base-package="com.company.project.ws"/> 

And your controllers might look like

 package com.company.project.ws; @Controller public class Service1 { // your controller methods } 
+5
source

The other approach (very simple and simple) that I implemented was to define multiple dispatcher servlets and then display different URLs for each servlet. Servlets share the Spring root context and, in addition, have their own bean definitions. Read more in this Java doc .

So my web.xml looks like this:

 <servlet> <servlet-name>flex</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Mappings for BlazeDS requests --> <servlet-mapping> <servlet-name>flex</servlet-name> <url-pattern>/spring/messagebroker/*</url-pattern> </servlet-mapping> <!-- Second dispatcher servlet for Web Services API --> <servlet> <servlet-name>wsapi</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>wsapi</servlet-name> <url-pattern>/spring/ws/*</url-pattern> </servlet-mapping> 

Basically, I left the existing dispatch servlet as is and added a new servlet only for REST controllers with different URL mappings. Therefore, I can control the URL of these servlets separately. After that, I no longer need to specify a URL prefix for each controller.

0
source

All Articles