当前位置: 首页 > 知识库问答 >
问题:

使用Spring引导泽西启动器部署为WAR时映射冲突

上官联
2023-03-14

我们将Spring Boot与其泽西Starter一起使用,并将其部署为WAR,以编程方式部署到另一个应用程序的嵌入式Tomcat中。

在我们的应用程序启动后,在某些环境中,会发生映射冲突并记录如下:

o.g.j.s.i.JerseyServletContainerInitializer : Mapping conflict. A Servlet registration exists with same mapping as the Jersey servlet application, named com.vidal.pmsi.config.PmsiResourceConfiguration, at the servlet mapping, /*.

资源配置如下:

@ApplicationPath("/")
@ExposedApplication
@Component
public class PmsiResourceConfiguration extends ResourceConfig {

   public PmsiResourceConfiguration() {
      packages("com.vidal.pmsi.api");
      packages("com.vidal.pmsi.config");
      property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
      property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
   }
}

据我所知,Spring Boot泽西Starter将注册一个名为“jerseyServlet”的servlet映射到“/*”。

在某些环境中,泽西自己的JerseyServletContainerLaunalizer将在SpringApplication启动后触发,由于现有的jerseyServlet映射,无法注册PmsiResourceConfiguration

这是一个问题,因为我们自己的开源库在启动时尝试(并崩溃)获取上下文路径:

// compile-time generated Linkers.java
@WebListener
@Generated("fr.vidal.oss.jax_rs_linker.LinkerAnnotationProcessor")
public final class Linkers implements ServletContextListener {
    private static String contextPath = "";
    private static String applicationName = ApplicationName.get();

    @Override
    public void contextInitialized(ServletContextEvent sce) {
         //applicationName = FQCN of PmsiResourceConfiguration
        contextPath = ContextPaths.contextPath(sce.getServletContext(), applicationName);
    }

    // [...]
}
// ContextPaths.java
public static String contextPath(ServletContext servletContext, String registeredKey) {
    // registeredKey is therefore the FQCN of PmsiResourceConfiguration
    String mappedPath = stripWildcard(servletContext.getServletRegistration(registeredKey).getMappings().iterator().next());
    return servletContext.getContextPath() + mappedPath;
}

最后一段代码将失败,因为注册的资源配置类没有映射(只有一个映射用于“jerseyServlet”键)。

当没有报告任何映射冲突时,这不会失败。为什么?

共有1个答案

毛峻
2023-03-14

我遇到了一个类似的问题,我有一个带有泽西JAX-RS Webservice的Spring Boot应用程序。使用嵌入式Tomcat时一切正常,但当我尝试在相同版本的常规Tomcat(Tomcat8)上部署战争时,一切都变得一团糟。

问题在于,默认情况下,嵌入式Tomcat不会在jar文件中扫描ServletContainerInitializer,但常规的Tomcat会扫描,并且与Spring设置的ServletContainer/Config冲突。

除了排除包含 JerseyServletContainerInitializer 的 jar 之外,我还找到了一个选项来告诉 tomcat 过滤掉这个特定的 ServletContainerInitializer (SCI)。在上下文中设置 containerSciFilter 属性有助于:

<Context containerSciFilter="JerseyServletContainerInitializer">
...
</Context>

我没有在我的META-INF/services中定义任何SCI,但是包含JerseySCI的jar已经定义了它,并且它位于Tomcat可以找到的正确路径上。

考虑到这是最接近matchinb的问题,如果没有答案,我不会重新发布我的问题,并试图回答这个问题,因为我相信原因是一样的。

 类似资料:
  • 正在尝试在linux服务器上部署spring启动应用程序 r,“线程名称”:“localhost-startStop-1”,“级别”:“错误”,“级别值”:40000,“堆栈跟踪”:“组织”。springframework。豆。工厂BeanCreationException:创建名为“org”的bean时出错。springframework。验证。Bean验证。OptionalValidatorF

  • 无法在weblogic服务器上部署jersey REST,是否存在特定于weblogic的REST服务配置? 完全堆栈跟踪- 2018年4月25日下午7:01:53。太阳运动衫服务器impl。应用WebApplicationImpl启动信息:启动泽西岛应用程序,版本“泽西岛:1.2 05/07/2010 02:11 PM”2018年4月25日7:01:53 PM com。太阳运动衫服务器impl。

  • 我将 Jackson 2.7.5 与 Jersey 2.25.1 配合使用。我正在尝试修复现有的生产代码,当它在JSON输入中获得意外字段时,它现在因“无法识别的PropertyException:无法识别的字段”而失败。 在研究这个问题时,我发现了几篇旧帖子(5年),建议各种修复方法与我当前的代码非常不同。我不太关注这些,因为它们是针对旧版本的Jackson/Jersey。最近的建议,包括泽西岛

  • 编辑:这是ackson jaxrs-base早期版本中的一个问题,已在jackson-jaxrs-base-2.8中解决。https://github.com/FasterXML/jackson-jaxrs-providers/issues/22 我正在努力解决这个问题,只有mapper例外。我想映射com的所有子级。fasterxml。杰克逊。果心JsonProcessingException在

  • 我需要在Jersey ServletContainer中热部署和取消部署资源。 如果我看一下源代码: 从WebComponent构造函数引发ModelValidationException。之后,任何重新加载的调用都会导致onShutdown方法发生异常,这是由ServiceLocatorImpl的preDestroy方法中的checkState引起的。 我可以通过忽略验证错误来避免异常

  • 我使用以下调试配置: 当我构建/Artifacts...时,IntelliJ IDEA创建了.war文件。 我谷歌了一些事情,但不适合我的场景。这里没有tomcat应用程序上下文正确,没有war文件不正确。 我的项目不是web.xml文件。但是我添加了web.xml,而不是运气。 我将tomcat7-maven-plugin追加到pom.xml文件中。不是效果。 为什么IntelliJ IDEA启