gpt4 book ai didi

java - Spring Boot 多模块 servletDispatchers

转载 作者:IT老高 更新时间:2023-10-28 13:57:51 26 4
gpt4 key购买 nike

我有以下项目结构

-Project
|-config
| |-modules
| |-admin
| |-web
|- platform

Platform是包含spring-boot启动类的项目,Platform 依赖于 config 并且 config 依赖于目录 modules 中的所有内容Platform 也是使用 mvn spring-boot:run 命令启动的模块。

我想要完成的事情是模块 admin 和 web(都是 web 应用程序)有自己的映射,比如

  • /管理员
  • /网络

以下代码表示管理模块中的一个 Controller ,web模块也包含一个类似的 Controller (重点)

@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. Using multiple dispatcher servlets / web contexts with spring boot
  2. Spring Boot (JAR) with multiple dispatcher servlets for different REST APIs with Spring Data REST
  3. 还有很多谷歌搜索

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

我得到一个 Ambiguous mapping found 异常,说两个 Controller 方法分派(dispatch)到同一个路径“/”

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

当我禁用两个 Controller 时,我看到/web 和/admin dispatchServlets 被映射。

所以我理解异常,但我不明白如何解决这个问题。对我来说,我必须每个模块都可以做到这一点,我不想使用它来映射它

@RequestMapping("/admin")

在 Controller 类上

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

所以我的问题是:达到我的目标的最佳方法是什么,或者我是否试图将 spring-boot 用于它不适合的东西。

编辑概念证明会很好

最佳答案

所以我找到了解决方案。你可以看看这里link

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

对于一个完整的例子,只需 checkout 项目并检查代码

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

关于java - Spring Boot 多模块 servletDispatchers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30313206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com