您可以通过传递多个值将所有请求映射到一个请求映射。
@RequestMapping(value = {"/aaa/xxx", "/bbb/xxx", "/ccc/xxx"}, method = RequestMethod.POST)
public String foo() {}
只需更改映射即可web.xml处理所有类型的请求dispatcher
小服务程序。
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
您可以根据应用程序需求或网络流程定义不同的控制器。如果需要,您可以移动实用程序类中的通用代码。
@RequestMapping("/aaa")
public class AAAController {
@RequestMapping(value = "/xxx", method = RequestMethod.POST)
public String foo() {
// call to common utility function
}
// other methods
}
@RequestMapping("/bbb")
public class BBBController {
@RequestMapping(value = "/xxx", method = RequestMethod.POST)
public String foo() {
// call to common utility function
}
// other methods
}
@RequestMapping("/ccc")
public class CCCController {
@RequestMapping(value = "/xxx", method = RequestMethod.POST)
public String foo() {
// call to common utility function
}
// other methods
}
春季阅读更多内容Web MVC 框架文档 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
您也可以通过编程方式配置它
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet());
registration.setLoadOnStartup(1);
registration.addMapping("/*");
}
}