Spring Boot多模块ServletDispatchers

Maikel Bollemeijer:

我有以下项目结构

-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模块也有类似的情况

我已经尝试了工具的一些给定的答案。

  1. 在Spring Boot中使用多个分派器Servlet / Web上下文
  2. Spring Boot(JAR)具有多个调度程序servlet,用于带有Spring Data REST的不同REST API
  3. 还有很多谷歌搜索

当我让组件扫描时,请扫描两个模块(删除ComponentScan过滤器)

我得到一个模糊映射发现异常,说两个控制器方法都调度到同一路径“ /”

但是,当在其中一个模块上禁用组件扫描时,则实际上管理模块会映射到/ admin。

当我同时禁用两个控制器时,我看到/ web和/ admin dispatchServlets已映射。

因此,我了解例外情况,但不了解如何解决此问题。对我来说,必须按模块执行此操作,并且我不想使用

@RequestMapping("/admin")

在控制器类上

我还尝试在application.properties中指定contextPath和servletPath

所以我的问题是:什么是达到我的目标的最佳方法,或者我是在尝试使用spring-boot来解决某些问题。

编辑概念证明会很好

Maikel Bollemeijer:

所以我找到了解决方案。您可以在这里查看此链接

您必须在主应用程序中注册调度程序servlet,并且不要使用@SpringBootApplication批注。

有关完整示例,请签出项目并检查代码

编辑:本周晚些时候请提供详细的答案

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章