Only set init param to disable Jersey when Jersey is present

Fixes gh-45289
This commit is contained in:
Andy Wilkinson 2025-04-25 14:45:50 +01:00
parent c420786eed
commit f1fefc5ff6
2 changed files with 30 additions and 4 deletions

View File

@ -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", "<NONE>");
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", "<NONE>");
}
}
}

View File

@ -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("<NONE>");
}
@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)