From f1fefc5ff62d2c1e894cc8bc87775e0d12ca3f4a Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 25 Apr 2025 14:45:50 +0100 Subject: [PATCH] Only set init param to disable Jersey when Jersey is present Fixes gh-45289 --- .../jersey/JerseyAutoConfiguration.java | 9 ++++--- .../jersey/JerseyAutoConfigurationTests.java | 25 ++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java index be0d886e57e..5a0be5b3a52 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java @@ -173,9 +173,12 @@ public class JerseyAutoConfiguration implements ServletContextAware { @Override public void onStartup(ServletContext servletContext) throws ServletException { - // We need to switch *off* the Jersey WebApplicationInitializer because it - // will try and register a ContextLoaderListener which we don't need - servletContext.setInitParameter("contextConfigLocation", ""); + if (ClassUtils.isPresent("org.glassfish.jersey.server.spring.SpringWebApplicationInitializer", + getClass().getClassLoader())) { + // We need to switch *off* the Jersey WebApplicationInitializer because it + // will try and register a ContextLoaderListener which we don't need + servletContext.setInitParameter("contextConfigLocation", ""); + } } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationTests.java index 448f7884746..add8e9d7e1d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,18 +16,25 @@ package org.springframework.boot.autoconfigure.jersey; +import java.util.Collections; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; import org.glassfish.jersey.server.ResourceConfig; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; +import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration.JerseyWebApplicationInitializer; import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.boot.testsupport.classpath.ClassPathExclusions; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.mock.web.MockServletContext; import org.springframework.web.filter.RequestContextFilter; import static org.assertj.core.api.Assertions.assertThat; @@ -106,6 +113,22 @@ class JerseyAutoConfigurationTests { .stream() .filter(JakartaXmlBindAnnotationIntrospector.class::isInstance)).isEmpty(); }); + + } + + @Test + void webApplicationIntializerDisablesJerseysWebApplicationInitializer() throws ServletException { + ServletContext context = new MockServletContext(); + new JerseyWebApplicationInitializer().onStartup(context); + assertThat(context.getInitParameter("contextConfigLocation")).isEqualTo(""); + } + + @Test + @ClassPathExclusions("jersey-spring6-*.jar") + void webApplicationInitializerHasNoEffectWhenJerseyIsAbsent() throws ServletException { + ServletContext context = new MockServletContext(); + new JerseyWebApplicationInitializer().onStartup(context); + assertThat(Collections.list(context.getInitParameterNames())).isEmpty(); } @Configuration(proxyBeanMethods = false)