我有以下项目结构
-Project
|-config
| |-modules
| |-admin
| |-web
|- platform
Platform是包含spring-boot起始类的项目,Platform依赖于config,而config依赖于目录模块中的所有内容。Platform也是使用mvn spring-boot:run命令入门的模块。
我要完成的事情是,模块admin和web(两个web应用程序)都有自己的映射,例如
以下代码代表admin模块中的控制器,Web模块还包含一个类似的控制器(这就是重点)
@Controller
public class AdminController {
@RequestMapping("/")
public String adminController() {
return "admin";
}
}
这里是一些用于管理模块配置的代码
@Configuration
public class Config implements EmbeddedServletContainerCustomizer {
@Autowired
protected WebApplicationContext webApplicationContext;
@Autowired
protected ServerProperties server;
@Autowired(required = false)
protected MultipartConfigElement multipartConfig;
protected DispatcherServlet createDispatcherServlet() {
AnnotationConfigEmbeddedWebApplicationContext webContext = new AnnotationConfigEmbeddedWebApplicationContext();
webContext.setParent(webApplicationContext);
webContext.scan("some.base.package");
return new DispatcherServlet(webContext);
}
protected ServletRegistrationBean createModuleDispatcher(DispatcherServlet apiModuleDispatcherServlet) {
ServletRegistrationBean registration =
new ServletRegistrationBean(apiModuleDispatcherServlet,
"/admin");
registration.setName("admin");
registration.setMultipartConfig(this.multipartConfig);
return registration;
}
@Bean(name = "adminsServletRegistrationBean")
public ServletRegistrationBean apiModuleADispatcherServletRegistration() {
return createModuleDispatcher(createDispatcherServlet());
}
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setContextPath("/admin");
}
}
Web模块也有类似的情况
我已经尝试了工具的一些给定的答案。
当我让组件扫描时,请扫描两个模块(删除ComponentScan过滤器)
我得到一个模糊映射发现异常,说两个控制器方法都调度到同一路径“ /”
但是,当在其中一个模块上禁用组件扫描时,则实际上管理模块会映射到/ admin。
当我同时禁用两个控制器时,我看到/ web和/ admin dispatchServlets已映射。
因此,我了解例外情况,但不了解如何解决此问题。对我来说,必须按模块执行此操作,并且我不想使用
@RequestMapping("/admin")
在控制器类上
我还尝试在application.properties中指定contextPath和servletPath
所以我的问题是:什么是达到我的目标的最佳方法,或者我是在尝试使用spring-boot来解决某些问题。
编辑概念证明会很好
所以我找到了解决方案。您可以在这里查看此链接
您必须在主应用程序中注册调度程序servlet,并且不要使用@SpringBootApplication批注。
有关完整示例,请签出项目并检查代码
编辑:本周晚些时候请提供详细的答案
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句